欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Swift利用純代碼實(shí)現(xiàn)時鐘效果實(shí)例代碼

 更新時間:2018年05月03日 11:24:02   作者:OneSwift  
這篇文章主要給大家介紹了關(guān)于Swift利用純代碼實(shí)現(xiàn)時鐘效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用swift具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

在剛開始學(xué)習(xí)iOS開發(fā)時,我制作了OneClock,它除了使用最多的翻頁時鐘效果,還擁有最常見的時鐘樣式。

今天用一個很簡單的方式為大家展示如何實(shí)現(xiàn)時鐘效果。

1.分別創(chuàng)建時針、分針、秒針

2.隨著時間改變進(jìn)行對應(yīng)旋轉(zhuǎn)

一、創(chuàng)建時針、分針、秒針

分別創(chuàng)建三個指針的同時,我們初始化了他們的位置,也就是在12點(diǎn)的方向。

這里我只貼出創(chuàng)建的代碼,在使用的過程中可以根據(jù)需要進(jìn)行放置。其中hourLength、minuteLength、secondLength分別代表三個指針的長度,在實(shí)際使用中根據(jù)頁面情況進(jìn)行調(diào)整大小。

//創(chuàng)建一個時針VIEW
 let hourView:UIView! = UIView.init()
 hourView.center = self.view.center
 hourView.layer.bounds = CGRect(x: 0, y: 0, width: 2, height: hourLength)
 hourView.backgroundColor = PolarTheme.bottomColor
 hourView.layer.anchorPoint = CGPoint(x: 1, y: 1)
 hourView.layer.cornerRadius = 1
 self.hourView = hourView
 self.view.addSubview(self.hourView)

 //創(chuàng)建一個分針VIEW
 let minuteView:UIView! = UIView.init()
 minuteView.center = self.view.center
 minuteView.layer.bounds = CGRect(x: 0, y: 0, width: Int(1.5), height: Int(minuteLength))
 minuteView.backgroundColor = PolarTheme.bottomColor
 minuteView.layer.anchorPoint = CGPoint(x: 1, y: 1)

 self.minuteView = minuteView
 self.view.addSubview(self.minuteView)


 //創(chuàng)建一個秒針VIEW
 let secondView:UIView! = UIView.init()
 secondView.center = self.view.center
 secondView.layer.bounds = CGRect(x: 0, y: 0, width: 1, height: secondLength)
 secondView.backgroundColor = PolarTheme.topColor
 secondView.layer.anchorPoint = CGPoint(x: 1, y: 1)
 self.secondView = secondView
 self.view.addSubview(self.secondView)

二、時間跟蹤器

創(chuàng)建指針只是時鐘的第一步,之后我們需要用時間來改變對應(yīng)的指針位置。所以在創(chuàng)建指針之后,我們需要緊跟著一個時間監(jiān)視器,通過move函數(shù)來調(diào)整指針方向。

先定義Timer

var timer:Timer!

再為Timer綁定函數(shù),時間間隔為0.2秒執(zhí)行一次move

timer = Timer.scheduledTimer(timeInterval: 0.2,target:self,selector:#selector(PolarViewController.move),userInfo:nil,repeats:true)

三、改變指針位置

實(shí)現(xiàn)原理是,先在函數(shù)move()中定義每個指針旋轉(zhuǎn)的大小,再進(jìn)行周期性刷新位置。

func move(){
 var angleSecond:CGFloat = CGFloat(Double.pi * 2/60.0)
 var angleMinute:CGFloat = CGFloat(Double.pi * 2/60.0)
 var angleHour:CGFloat = CGFloat(Double.pi * 2/12.0)

 //當(dāng)前時間
 let date = Date()
 let calendar = NSCalendar.current

 let secondInt = CGFloat(calendar.component(.second, from: date))
 let minuteInt = CGFloat(calendar.component(.minute, from: date))
 let hourInt = CGFloat(calendar.component(.hour, from: date))

 //計(jì)算旋轉(zhuǎn)角度
 angleSecond = angleSecond * secondInt
 angleMinute = angleMinute * minuteInt + angleSecond/60.0
 angleHour = angleHour * hourInt + angleMinute/60.0

 //保持中心點(diǎn)
 self.secondView.center = self.view.center
 self.minuteView.center = self.view.center
 self.hourView.center = self.view.center
 //旋轉(zhuǎn)各自的角度
 self.secondView.transform = CGAffineTransform(rotationAngle: angleSecond)
 self.minuteView.transform = CGAffineTransform(rotationAngle: angleMinute)
 self.hourView.transform = CGAffineTransform(rotationAngle: angleHour)
}

四、手機(jī)旋轉(zhuǎn)后的頁面刷新

最后增加在旋轉(zhuǎn)模式下的自動刷新,幫助修正顯示。

override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation, duration: TimeInterval) {
 move()
}

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

最新評論