在控制器内实现两个变量
//验证码倒计时
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
}
}