Swift 自己写定时器
2015-11-17 19:01
671 查看
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, 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
}
}
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, 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
}
}
相关文章推荐
- Swift基本语法快速一览
- swift 利用协议进行值传递
- swift 利用协议进行值传递
- Swift自学_数据类型
- swift之枚举
- swift 快速奔跑的兔几 本节的内容是:使用文件系统存储数据
- swift 快速奔跑的兔几 本节的内容是:数据存储NSUserDefaults
- swift之闭包(closure)
- swift 快速奔跑的兔几 本节的内容是:照片库 第二说
- swift 内存管理,WEAK 和 UNOWNED
- 理解 OpenStack Swift (3):监控和一些影响性能的因素 [Monitoring and Performance]
- swift 快速奔跑的兔几 本节的内容是:使用照片库
- swift循环
- swift之函数(functions)
- swift 自定义购物车计数器
- swift 快速奔跑的兔几 本节的内容是:用AVAdioPlayer播放声音 语音合成
- 浅谈swift中的那些类,结构以及初始化的操作
- swift学习笔记->类
- iOS --- 使用Shimmer来在Swift工程中添加微光闪烁的特效
- swift学习笔记->函数