Swift 3.0獲取驗證碼實現方式

聲明一個計時器


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

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容