【Swift3.0】iOS Swift 驗(yàn)證碼按鈕倒計(jì)時(shí)

定義變量

 
    public var timerVerify:Timer!
    public var verifyButton:UIButton!
    public let VERIFY_TIME_EXPIRY = 31
    public var timeCount:Int = 31

調(diào)用完成獲取驗(yàn)證碼接口后,啟動(dòng)計(jì)時(shí)器

    public func startVerifyTimer(){
//        self.verifyButton.titleLabel?.adjustsFontSizeToFitWidth = true
        refreshVerifyBtnDisplay()
        timerVerify = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(BaseViewController.refreshVerifyBtnDisplay), userInfo: nil, repeats: true)
    }
    

異步刷新頁面按鈕的文字

    public func refreshVerifyBtnDisplay(){
        UIView.performWithoutAnimation({
            timeCount = timeCount - 1
            if timeCount==0 {
                self.verifyButton.isEnabled = true
                self.verifyButton.backgroundColor = UIColor.colorWithHexString(hex: "#A80000")
                self.verifyButton.setTitle(String.init(format: "send.verify.code".localized()), for: .normal)
                self.timerVerify.invalidate()
                self.timerVerify = nil
                self.timeCount = VERIFY_TIME_EXPIRY
                self.verifyCode = ""
            }else{
                
                self.verifyButton.isEnabled = false
                self.verifyButton.backgroundColor = UIColor.colorWithHexString(hex: "#666666")
                self.verifyButton.setTitle(("send.verify.code".localized()+"("+String.init(timeCount)+")"), for: .normal)
                
            }
            self.verifyButton.layoutIfNeeded()
        })
        
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,300評(píng)論 25 708
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,680評(píng)論 19 139
  • 刺猬運(yùn)西瓜 在一個(gè)夏日炎炎的下午,果園的果子紛紛從樹上跳了下來,鋪滿了一地。刺猬弟弟看...
    向著陽光00閱讀 659評(píng)論 0 0
  • 你也許不知道自己到底想要什么 你只知道自己討厭什么 也許你是一個(gè)感覺很快樂的人
    吹吹牛閱讀 156評(píng)論 0 0

友情鏈接更多精彩內(nèi)容