Swift3.0 Timer

Swift2.2到Swift3.0不知道几家欢喜几家愁,对于项目是纯Swift的我,反正是想艹阿婆他娘的。

2016年9月18号更新到了Xcode8,第二天将项目迁移到2.3。咦。。。好像没有改多少东西嘛!发了一个版本后,老大继续迁移到Swift3.0,我艹,什么玩意,999+个红。搞啊搞啊搞。。。

好了,废话不多说了,重点:

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()
        }
        // 启动定时器
        dispatch_resume(timer!)
    }

private func deinitTimer() {
        if let time = self.timer {
            dispatch_source_cancel(time)
            timer = nil
        }
    }

迁移器自动转换的版本就不贴出来了,浪费您宝贵的时间。

swift 3.0 Timer
private var timer: DispatchSourceTimer?
var pageStepTime: DispatchTimeInterval = .seconds(5)

// deadline 结束时间
// interval 时间间隔
// leeway  时间精度
func setTheTimer() {
        timer = DispatchSource.makeTimerSource(queue: .main)
        timer?.scheduleRepeating(deadline: .now() + pageStepTime, interval: pageStepTime)
        timer?.setEventHandler {
            self.yourMethod()
        }
        // 启动定时器
        timer?.resume()
    }

func deinitTimer() {
        if let time = self.timer {
            time.cancel()
            timer = nil
        }
    }

反正各种谷歌,各种百度,就找到这一个
TimerTest_Swift3

SwiftTimer 手机尾号为1193的朋友给的,非常感谢
Each 定时器

最后附上自己[swift2.3] []版本和[swift3.0] []版本多线程的学习代码
[swift2.3]: https://github.com/huangboju/Moots/tree/master/Thread
[swift3.0]: https://github.com/huangboju/Moots/tree/master/Thread

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 因为要结局swift3.0中引用snapKit的问题,看到一篇介绍Xcode8,swift3变化的文章,觉得很详细...
    uniapp阅读 4,512评论 0 12
  • 写在前面 收到一些小伙伴的来信,觉得可能下边没有表达清楚,先把大家关心的要点在此进行总结,有兴趣的可以看看下边的研...
    光无影阅读 11,086评论 40 61
  • 不知道从什么时候开始,内心脱离团体。 一起做事,但是心却不在一处,就像上周韩国艺术家分享的身体空间艺术那样,政治会...
    林大怡葩阅读 273评论 0 1
  • 1. 妈妈忽然说要给我扎辫子。 在耳旁两侧分别扎了麻花辫,后面再绑了个马尾。 妈妈绑完以后自己说:看起来...
    施与树阅读 743评论 0 0
  • 有人说,只有亲人或亲近朋友的离世才能让你体会到切肤之痛,否则别人的离开你都不能感同身受。我深以为然。 汶川地震我也...
    狗大鱼阅读 435评论 0 0