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+NSTimer)