微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐