声明一个计时器
fikeprivate var countdownTimer: Timer?
计时剩余秒数
fileprivate var remainingSeconds: Int =0 {will Set {
UIView.beginAnimations(nil, context:nil)
UIView.setAnimationDuration(1)
self.getCodeBtn.setTitle("\(newValue)秒", for:UIControlState.normal)
UIView.commitAnimations()
ifnewValue <=0{
getCodeBtn.isEnabled=true
getCodeBtn.setTitle("重发验证码", for:UIControlState.normal)
}
}
}
声明一个变量来开关计时器
fileprivate var isCounting =false{
willSet{
ifnewValue {
countdownTimer=Timer.scheduledTimer(timeInterval:1, target:self, selector:#selector(RegisterViewController.updateTime), userInfo:nil, repeats:true)
remainingSeconds=60
}else{
getCodeBtn.isEnabled=true
countdownTimer?.invalidate()
countdownTimer=nil
}
getCodeBtn.isEnabled=!newValue
}
}
@objc fileprivate func updateTime(){
remainingSeconds=remainingSeconds-1
}
网友评论