UIScrollView+NSTimer

2018-08-13  本文已影响0人  _狸约约

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()
}
上一篇下一篇

猜你喜欢

热点阅读