Swift定时器的几种实现方式
方式一:
使用performSelector实现
@objc func getCodeAction(sender:UIButton) {
count = 60
self.performSelector(inBackground: #selector(timerThread), with: nil)
//获取验证码
}
@objc func timerThread() {
let timeCount = count
for _ in 0..<timeCount {
count = count - 1
self.performSelector(onMainThread: #selector(updateCodeBtn), with: self, waitUntilDone: true)
sleep(1)
}
}
@objc func updateCodeBtn() {
//处理更新
}
方式二: 使用Timer,在iOS10后可使用
Timer.scheduledTimer(withTimeInterval: TimeInterval(1), repeats: true, block:{
(timer: Timer) -> Void in
})
本文由 创作,采用 知识共享署名4.0 国际许可协议进行许可。本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为: 2021/06/10 03:19