在收藏夹里面发现了自己很久前收藏的关于ios动画的中文资料,前进的道路上,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:锚点,通过它,防止三条线以自己中心位置旋转
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()
根据获取到的时间,位置旋转
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))
}