.NET/C# からすると、全体の設計が違っているので、とても戸惑いました。最初は、UIViewに直接描画するのかと思いましたが、秒、分、時それぞれごとにCAShapeLayer に時計の針を描画して、レイヤーの原点を translateしてセンタリングして、さらに時間に合わせて rotate し、で重ね合わせる方法が良さそうです。秒針をスムースに動かすのであれば、Animation を適用すればOK。1
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
import UIKit class UIClockView: UIView { var secLayer: CAShapeLayer! var minLayer: CAShapeLayer! var hourLayer: CAShapeLayer! override init(frame: CGRect) { super.init(frame: frame) addHourLayer() addMinLayer() addSecLayer() } required init?(coder: NSCoder) { super.init(coder: coder) addHourLayer() addMinLayer() addSecLayer() } func drawClock() { let date = Date() let calendar = Calendar.current let hour = Int(calendar.component(.hour, from: date)) let min = Int(calendar.component(.minute, from: date)) let sec = Int(calendar.component(.second, from: date)) drawHourLayer(hour: hour, min: min) drawMinLayer(min: min, sec: sec) drawSecLayer(sec: sec) } func addHourLayer() { hourLayer = CAShapeLayer() hourLayer.isHidden = true hourLayer.strokeColor = UIColor.red.cgColor hourLayer.lineWidth = 5.0 let line = CGMutablePath() line.move(to: CGPoint(x:0,y:0)) line.addLine(to:CGPoint(x:80,y:0)) hourLayer.path = line self.layer.addSublayer(hourLayer) } func drawHourLayer(hour: Int, min: Int) { let radian = 2.0 * CGFloat.pi * (CGFloat(hour) + CGFloat(min) / 60.0) / 12.0 - 0.5 * CGFloat.pi; let translation = CGAffineTransform(translationX: self.bounds.width/2, y: self.bounds.height/2) let rotation = CGAffineTransform(rotationAngle: radian) let trans = rotation.concatenating(translation) hourLayer.setAffineTransform(trans) hourLayer.isHidden = false } func addMinLayer() { minLayer = CAShapeLayer() minLayer.isHidden = true minLayer.strokeColor = UIColor.blue.cgColor minLayer.lineWidth = 3.0 let line = CGMutablePath() line.move(to: CGPoint(x:0,y:0)) line.addLine(to:CGPoint(x:80,y:0)) minLayer.path = line self.layer.addSublayer(minLayer) } func drawMinLayer(min: Int, sec: Int) { let radian = (2.0 * CGFloat.pi * CGFloat(min) + CGFloat(sec) / 60.0) / 60.0 - 0.5 * CGFloat.pi; let translation = CGAffineTransform(translationX: self.bounds.width/2, y: self.bounds.height/2) let rotation = CGAffineTransform(rotationAngle: radian) let trans = rotation.concatenating(translation) minLayer.setAffineTransform(trans) minLayer.isHidden = false } func addSecLayer() { secLayer = CAShapeLayer() secLayer.isHidden = true secLayer.strokeColor = UIColor.black.cgColor secLayer.lineWidth = 1.0 let line = CGMutablePath() line.move(to: CGPoint(x:0,y:0)) line.addLine(to:CGPoint(x:100,y:0)) secLayer.path = line self.layer.addSublayer(secLayer) } func drawSecLayer(sec: Int) { let radian = 2.0 * CGFloat.pi * CGFloat(sec) / 60.0 - 0.5 * CGFloat.pi; let translation = CGAffineTransform(translationX: self.bounds.width/2, y: self.bounds.height/2) let rotation = CGAffineTransform(rotationAngle: radian) let trans = rotation.concatenating(translation) secLayer.setAffineTransform(trans) secLayer.isHidden = false } } |