Swift-时钟

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

Swift-时钟_第1张图片
时钟.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: RunLoopMode.commonModes)

定时刷新:

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

你可能感兴趣的:(Swift-时钟)