swift 3.0 realizes the countdown function of SMS verification code


The following code is to share with you that swift 3.0 realizes the countdown function of SMS verification code. The specific example code is as follows:

class TCCountDown {
private var countdownTimer: Timer?
var codeBtn = UIButton()
private var remainingSeconds: Int = 0 {
  willSet {
    codeBtn.setTitle(" To obtain \(newValue) seconds ", for: .normal)
    if newValue <= 0 {
      codeBtn.setTitle(" Get verification code ", for: .normal)
      isCounting = false
    }
  }
}
var isCounting = false {
  willSet {
    if newValue {
      countdownTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateTime), userInfo: nil, repeats: true)
      remainingSeconds = 60
      codeBtn.setTitleColor(BtnCodeColor, for: .normal)
    } else {
      countdownTimer?.invalidate()
      countdownTimer = nil
      codeBtn.setTitleColor(MainColor, for: .normal)
    }
    codeBtn.isEnabled = !newValue
  }
}
@objc private func updateTime() {
  remainingSeconds -= 1
}
 }
 // A method is called
 var countDown = TCCountDown()// instantiation
 countDown.isCounting = true// Open countdown