在控制器内实现两个变量
//验证码倒计时
fileprivate var mdowntime = 60
fileprivate var mtimer : Timer?
实现注册计时器方法
fileprivate func resgistTimer() {
self.mSecorityCodeBtn.isEnabled = false
if mtimer != nil {
mtimer!.invalidate()
}
mtimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.actionforTimer), userInfo: nil, repeats: true)
mtimer?.fire()
}
func actionforTimer() {
if mdowntime == 0 {
mtimer?.invalidate()
mtimer = nil
mdowntime = 60
self.mSecorityCodeBtn.isEnabled = true
self.mSecorityCodeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
self.mSecorityCodeBtn.setTitle("获取验证码", for: UIControlState())
self.mSecorityCodeBtn.backgroundColor = UIColor.white
}else{
self.mSecorityCodeBtn.isEnabled = false
self.mSecorityCodeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
self.mSecorityCodeBtn.backgroundColor = AppConfig.BarbackGrayColor
self.mSecorityCodeBtn.setTitle("\(mdowntime)s", for: UIControlState())
mdowntime -= 1
}
}
本文介绍了一种使用Swift语言实现验证码倒计时功能的方法。通过定义变量来控制倒计时的时间,并使用Timer进行时间更新,同时禁用按钮直至倒计时结束。此功能适用于移动应用开发中验证码发送后的等待阶段。
1564

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



