UIScrollView+NSTimer

UIScrollView滚动时,Timer不失效的方法
1、改变当前RunLoop的mode

let timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (timer) in
     //code
}
RunLoop.current.add(timer, forMode: .commonModes)

2、在主线程中定义Timer

DispatchQueue.main.async {
     let timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (timer) in
        //code
     }
     RunLoop.current.run()
}

3、在子线程中定义Timer

DispatchQueue.global().async {
   let timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (timer) in
         //code 若有UI操作,需要回到主线程
    }
    RunLoop.current.add(timer, forMode: .defaultRunLoopMode)
    RunLoop.current.run()
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • UIScrollView 什么是UIScrollView 移动设备的屏幕大小是极其有限的,因此直接展示在用户眼前的...
    SoManyDumb阅读 184评论 0 0
  • Runloop 是和线程紧密相关的一个基础组件,是很多线程有关功能的幕后功臣。尽管在平常使用中几乎不太会直接用到,...
    jackyshan阅读 9,888评论 10 75
  • 1.ios高性能编程 (1).内层 最小的内层平均值和峰值(2).耗电量 高效的算法和数据结构(3).初始化时...
    欧辰_OSR阅读 29,591评论 8 265
  • runtime 和 runloop 作为一个程序员进阶是必须的,也是非常重要的, 在面试过程中是经常会被问到的, ...
    made_China阅读 1,226评论 0 7
  • runtime 和 runloop 作为一个程序员进阶是必须的,也是非常重要的, 在面试过程中是经常会被问到的, ...
    SOI阅读 21,848评论 3 63