Swift 获取手机验证码倒计时效果

    
    
    var countDownTime:Timer?
    
    var  remainSeconds: Int = 0{
        willSet{
            self.getCodeButton.setTitle("\(newValue)秒后重新获取", for: .normal)
            self.getCodeButton.isEnabled = false
            
            if newValue <= 0 {
                self.getCodeButton.setTitle("重新获取", for: .normal)
                self.getCodeButton.isEnabled = true
            }
            
        }
    }
    
    
    
    var isCounting = false{
        willSet{
            if newValue {
                countDownTime = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)
                remainSeconds = 60
            }
            
        }
    }

   @objc func updateTime(_timer:Timer){
        remainSeconds -= 1
    }
    

你可能感兴趣的:(Swift 获取手机验证码倒计时效果)