您好,欢迎来到伴沃教育。
搜索
您的当前位置:首页Swift-时钟

Swift-时钟

来源:伴沃教育

时钟的实现主要功能点有时针,分针,秒针旋转的角度,定时刷新,实现效果如下:

时钟.gif

时钟布局:

let bgView:UIView = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
        bgView.center = self.view.center
        self.view.addSubview(bgView)
        
        let clockImgView:UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
        clockImgView.image = UIImage(named: "clock")
        bgView.addSubview(clockImgView)
        
        hourHandView = UIView(frame: CGRect(x: 0, y: 0, width: 3, height: 60))
        hourHandView?.backgroundColor = UIColor.black
        hourHandView?.center = CGPoint(x: 75, y: 75)
        hourHandView?.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        bgView.addSubview(hourHandView!)
        
        minuteHandView = UIView(frame: CGRect(x: 0, y: 0, width: 2, height: 60))
        minuteHandView?.backgroundColor = UIColor.black
        minuteHandView?.center = CGPoint(x: 75, y: 75)
        minuteHandView?.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        bgView.addSubview(minuteHandView!)
        
        secondHandView = UIView(frame: CGRect(x: 0, y: 0, width: 1, height: 60))
        secondHandView?.backgroundColor = UIColor.red
        secondHandView?.center = CGPoint(x: 75, y: 75)
        secondHandView?.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        bgView.addSubview(secondHandView!)
        
        let displayLink:CADisplayLink = CADisplayLink(target: self, selector: #selector(self.runClock))
        displayLink.add(to: RunLoop.main, forMode: 

定时刷新:

 var calendar:Calendar = NSCalendar.current
        calendar.timeZone = NSTimeZone.local
        
        let date:Date = Date()
        
        let second:Int =  from: date)
        let secondAngle:Double = (Double.pi * 2 / 60) * Double(second)
        secondHandView?.transform = CGAffineTransform(rotationAngle: CGFloat(secondAngle))
        
        let minute:Int =  from: date)
        let minuteAngle:Double = (Double.pi * 2 / 60) * Double(minute)
        minuteHandView?.transform = CGAffineTransform(rotationAngle: CGFloat(minuteAngle))
        
        let hour:Int =  from: date)
        let hourAngle:Double = (Double.pi * 2 / 12) * Double(hour)
        hourHandView?.transform = CGAffineTransform(rotationAngle: CGFloat(hourAngle))

Copyright © 2019- bangwoyixia.com 版权所有 湘ICP备2023022004号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务