Swift2.2到Swift3.0不知道幾家歡喜幾家愁,對(duì)于項(xiàng)目是純Swift的我,反正是想艸阿婆他娘的。
2016年9月18號(hào)更新到了Xcode8龙巨,第二天將項(xiàng)目遷移到2.3。咦熊响。旨别。。好像沒(méi)有改多少東西嘛汗茄!發(fā)了一個(gè)版本后秸弛,老大繼續(xù)遷移到Swift3.0,我艸,什么玩意递览,999+個(gè)紅叼屠。搞啊搞啊搞。非迹。环鲤。
好了,廢話不多說(shuō)了憎兽,重點(diǎn):
swift 2.3 Timer
private var timer = dispatch_source_t?()
func setTheTimer() {
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue())
let start = dispatch_time(DISPATCH_TIME_NOW, Int64(pageStepTime * NSEC_PER_SEC))
let interval = pageStepTime * NSEC_PER_SEC
dispatch_source_set_timer(timer!, start, interval, 0)
dispatch_source_set_event_handler(timer!) {
self.yourMethod()
}
// 啟動(dòng)定時(shí)器
dispatch_resume(timer!)
}
private func deinitTimer() {
if let time = self.timer {
dispatch_source_cancel(time)
timer = nil
}
}
遷移器自動(dòng)轉(zhuǎn)換的版本就不貼出來(lái)了,浪費(fèi)您寶貴的時(shí)間吵冒。
swift 3.0 Timer
private var timer: DispatchSourceTimer?
var pageStepTime: DispatchTimeInterval = .seconds(5)
// deadline 結(jié)束時(shí)間
// interval 時(shí)間間隔
// leeway 時(shí)間精度
func setTheTimer() {
timer = DispatchSource.makeTimerSource(queue: .main)
timer?.scheduleRepeating(deadline: .now() + pageStepTime, interval: pageStepTime)
timer?.setEventHandler {
self.yourMethod()
}
// 啟動(dòng)定時(shí)器
timer?.resume()
}
func deinitTimer() {
if let time = self.timer {
time.cancel()
timer = nil
}
}
反正各種谷歌纯命,各種百度,就找到這一個(gè)
TimerTest_Swift3
SwiftTimer 手機(jī)尾號(hào)為1193的朋友給的痹栖,非常感謝
Each 定時(shí)器
最后附上自己[swift2.3] []版本和[swift3.0] []版本多線程的學(xué)習(xí)代碼
[swift2.3]: https://github.com/huangboju/Moots/tree/master/Thread
[swift3.0]: https://github.com/huangboju/Moots/tree/master/Thread