方式一:
使用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
})
本文介绍了两种在iOS中实现验证码倒计时的方法。第一种是通过使用performSelector方法配合后台线程定时更新UI;第二种则是利用Timer类及其scheduledTimer方法来实现重复性的计时任务。
773

被折叠的 条评论
为什么被折叠?



