KotlinとSwiftでアナログ時計を作るーー長針、短針、秒針の更新処理(前)
こんにちは。川上です。
Kotlinでの時計針の回転処理がイマイチの状態なので、今の所、Swiftのアナログ時計を触っています。
で、長針、短針、秒針の回転が想定の軌跡で出来たSwift処理分(前編)です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | let l_HariImage = UIImage(named:"long_hari.png")! //長針 let s_HariImage = UIImage(named:"short_hari.png")! //短針 let byo_HariImage = UIImage(named:"byo_hari.png")! //秒針 var l_HariImageVw :UIImageView = UIImageView(); var s_HariImageVw :UIImageView = UIImageView(); var byo_HariImageVw :UIImageView = UIImageView(); // MARK: ⚡️--ライフサイクル:レイアウト処理終了メソッド override func viewDidLayoutSubviews() { print(debug:"****** viewDidLayoutSubviews() ***** ") // MARK:+++++ Clock Base Setting makeClockBaseSetting() } // MARK:+++++ Clock Base Setting func makeClockBaseSetting() { // -- 既存Objの削除 self.view.removeAllSubviews() // -- 表示親Viewサイズ let selfsize = self.view.frame.size // -- Imageの縦横サイズ let bsWorH = (selfsize.width > selfsize.height ? selfsize.height : selfsize.width ) - 20 let imgsize = CGSize(width: bsWorH, height: bsWorH) // -- 背景のImageView baseImageVw.frame = TotnCGRect.CGRectMake(0, 0, bsWorH, bsWorH) baseImageVw.center = self.view.center self.view.addSubview(baseImageVw) // -- 長針、短針、秒針のImageView l_HariImageVw = UIImageView(image:l_HariImage.resize(size: imgsize)) s_HariImageVw = UIImageView(image:s_HariImage.resize(size: imgsize)) byo_HariImageVw = UIImageView(image:byo_HariImage.resize(size: imgsize)) let hariss:[UIImageView] = [l_HariImageVw,s_HariImageVw,byo_HariImageVw] for (_ ,imgvw) in hariss.enumerated() { imgvw.frame = TotnCGRect.CGRectMake(0, 0, bsWorH, bsWorH) imgvw.center = self.view.center imgvw.contentMode = UIView.ContentMode.center self.view.addSubview(imgvw) } ・・・・ // -- 時刻表示 startTimeClock() } func startTimeClock() { // -- 現在時間の取得 let datetpl = TotnDate.convertNSDateToYMDHMS(date: Date() as NSDate) print("datetpl =\(datetpl)") // -- 回転角度を計算 let hour = 60 * datetpl.hh + datetpl.min let min = datetpl.min let sec = datetpl.sec let f_Hour:CGFloat = CGFloat(2 * CGFloat.pi * CGFloat(hour) / (60 * 12) ) let f_min:CGFloat = CGFloat(2 * CGFloat.pi * CGFloat(min) / 60 ) let f_sec:CGFloat = CGFloat(2 * CGFloat.pi * CGFloat(sec) / 60 ) let rotateHour = CGAffineTransform( rotationAngle: f_Hour) let rotateMin = CGAffineTransform( rotationAngle: f_min ) let rotateSec = CGAffineTransform(rotationAngle: f_sec ) s_HariImageVw.transform = rotateHour l_HariImageVw.transform = rotateMin byo_HariImageVw.transform = rotateSec } |
ImageViewのresizeやDateの取得関連には、次回のTimerでの更新処理へ、つ・づ・く。。。です。
iPhone用の動画はココです。
ではでは。