在收藏夾里面發(fā)現(xiàn)了自己很久前收藏的關(guān)于ios動畫的中文資料黔帕,前進(jìn)的道路上代咸,Core Animation是邁不過的坎
iOS Core Animation: Advanced Techniques中文譯本
//////////////////////////我是分割線/////////////////////////
效果:
代碼:
先定義三條線和一個定時器:
fileprivate var hourView : UIView!
fileprivate var minuteView : UIView!
fileprivate var secondView : UIView!
fileprivate var timer : Timer!
self.view.backgroundColor = UIColor.white
hourView = UIView.init(frame: CGRect.init(x: 100, y: 100, width: 5, height: 40))
hourView.backgroundColor = UIColor.red
view.addSubview(hourView)
minuteView = UIView.init(frame: CGRect.init(x: 101, y: 100, width: 3, height: 38))
minuteView.backgroundColor = UIColor.cyan
view.addSubview(minuteView)
secondView = UIView.init(frame: CGRect.init(x: 102, y: 100, width: 1, height: 36))
secondView.backgroundColor = UIColor.blue
view.addSubview(secondView)
//anchorPoint:錨點,通過它成黄,防止三條線以自己中心位置旋轉(zhuǎn)
hourView.layer.anchorPoint = CGPoint.init(x: 0.5, y: 0.9)
minuteView.layer.anchorPoint = CGPoint.init(x: 0.5, y: 0.9)
secondView.layer.anchorPoint = CGPoint.init(x: 0.5, y: 0.9)
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.tick), userInfo: nil, repeats: true)
timer?.fire()
根據(jù)獲取到的時間呐芥,位置旋轉(zhuǎn)
func tick(){
let date = Date()
let calendar = NSCalendar.current
let components = calendar.dateComponents([Calendar.Component.hour,Calendar.Component.minute,Calendar.Component.second], from: date)
let hoursAngle = Double(components.hour!) / 12.0 * Double.pi * 2.0
let minsAngle = Double(components.minute!) / 60.0 * Double.pi * 2.0
let secsAngle = Double(components.second!) / 60.0 * Double.pi * 2.0
hourView.transform = CGAffineTransform.init(rotationAngle: CGFloat(hoursAngle))
minuteView.transform = CGAffineTransform.init(rotationAngle: CGFloat(minsAngle))
secondView.transform = CGAffineTransform.init(rotationAngle: CGFloat(secsAngle))
}