Swift定時器

Snip20171225_4.png
代碼在這

// 定義需要計時的時間
        var timeCount = 60
        // 在global線程里創(chuàng)建一個時間源
        let codeTimer = DispatchSource.makeTimerSource(queue:DispatchQueue.global())
        // 設(shè)定這個時間源是每秒循環(huán)一次,立即開始
        codeTimer.scheduleRepeating(deadline: .now(), interval: .seconds(1))
        // 設(shè)定時間源的觸發(fā)事件
        codeTimer.setEventHandler(handler: {
            // 每秒計時一次
            timeCount = timeCount - 1
            
           
            // 時間到了取消時間源
            if timeCount <= 0 {
                
                codeTimer.cancel()
            }
            // 返回主線程處理一些事件,更新UI等等
            DispatchQueue.main.async {
                sender.setTitle("\(timeCount)s", for: UIControlState.normal)
            }
        })
        // 啟動時間源
        codeTimer.resume()
    }
        
簡單使用
Untit.gif
      override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(composeBtn)
        composeBtn.frame = CGRect(x: 100, y: 100, width: 150, height: 40)
    }
    
    
    fileprivate lazy var composeBtn:UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor.red
        btn.setTitle("獲取驗(yàn)證碼", for: UIControlState.normal)
        btn.addTarget(self, action: #selector(btnClick(sender:)), for: UIControlEvents.touchUpInside)
        return btn
    }()
    
    func btnClick(sender:UIButton) {
        // Do any additional setup after loading the view, typically from a nib.
        // 定義需要計時的時間
        var timeCount = 60
        // 在global線程里創(chuàng)建一個時間源
        let codeTimer = DispatchSource.makeTimerSource(queue:DispatchQueue.global())
        // 設(shè)定這個時間源是每秒循環(huán)一次,立即開始
        codeTimer.scheduleRepeating(deadline: .now(), interval: .seconds(1))
        // 設(shè)定時間源的觸發(fā)事件
        codeTimer.setEventHandler(handler: {
            // 每秒計時一次
            timeCount = timeCount - 1
            
            // 返回主線程處理一些事件,更新UI等等
            DispatchQueue.main.async {
                sender.isEnabled = false
                sender.backgroundColor = UIColor.lightGray
                sender.setTitle("\(timeCount)s", for: UIControlState.normal)
            }
            // 時間到了取消時間源
            if timeCount <= 0 {
                DispatchQueue.main.async {
                    sender.isEnabled = true
                    sender.backgroundColor = UIColor.red
                    sender.setTitle("重新獲取驗(yàn)證碼", for: UIControlState.normal)
                }
                codeTimer.cancel()
            }
         
        })
        // 啟動時間源
        codeTimer.resume()
    }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 開發(fā)中不可避免會用到注冊獲取驗(yàn)證碼,在驗(yàn)證碼定時器使用方面別出心裁,各有妙招,今天推薦一個比較實(shí)用的定時器使用方法...
    Kean_Qi閱讀 479評論 0 0
  • 1.定義全局 var timer = NSTimer?() var count = 10 var btn = UI...
    小_蠟筆閱讀 1,219評論 0 0
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,008評論 25 709
  • 想說都是雜言 粗略看到網(wǎng)傳演員QRL突然死亡 網(wǎng)傳 以為絕對是謠傳 沒想到點(diǎn)進(jìn)去后已經(jīng)確認(rèn)是事實(shí) 印象中此人該是陽...
    大風(fēng)小雨閱讀 315評論 0 0
  • Suppose an array sorted in ascending order is rotated at ...
    PeterHe888閱讀 289評論 0 0

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