美文网首页
UIScrollView+NSTimer

UIScrollView+NSTimer

作者: _狸约约 | 来源:发表于2018-08-13 16:16 被阅读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()
    }
    

    相关文章

      网友评论

          本文标题:UIScrollView+NSTimer

          本文链接:https://www.haomeiwen.com/subject/xdeubftx.html