Swift 自己写定时器

前端之家收集整理的这篇文章主要介绍了Swift 自己写定时器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

private var timer: dispatch_source_t? = nil

private func startTimer() {

let updateInterval: UInt64 = 1

let interval = updateInterval * NSEC_PER_SEC

let leeway: UInt64 = 300000000 // UInt64(0.3) * interval

let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0)


timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,queue)

if timer != nil {

dispatch_source_set_timer(timer!,dispatch_walltime(nil,0),interval,leeway)

dispatch_source_set_event_handler(timer!,{ () -> Void in

self.checkNewScreenshot()

})

}

dispatch_resume(timer!)

}

private func stopTimer() {

if timer != nil {

dispatch_suspend(timer!)

dispatch_resume(timer!)

timer = nil

}

}

原文链接:https://www.f2er.com/swift/325402.html

猜你在找的Swift相关文章