• swift




    mport UIKit
    
    /// 控制定时器的类
    class ZDTimerTool: NSObject {
        /// 定时器
    //    private var timer: Timer?
        /// GCD定时器
        private var GCDTimer: DispatchSourceTimer?
        /// GCD定时器的挂起状态
        private var isSuspend: Bool = false
        override init() {
            super.init()
        }
        deinit {
            // 对象在销毁前会销毁定时器,所以使用定时器应该设置全局的属性
    //        self.invaliTimer()
            self.invaliGCDTimer()
            DDLOG(message: "deinit: ZDTimerTool")
        }
    }
    /// GCD定时器相关方法
    extension ZDTimerTool{
        /// 初始化得到GCD定时器
        func DispatchTimer(timeInterval: TimeInterval , handleBlock:@escaping (() -> Void)) {
            if self.GCDTimer == nil {
                self.GCDTimer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)
                self.GCDTimer?.schedule(deadline: DispatchTime.now(), repeating: timeInterval)
                self.GCDTimer?.setEventHandler{
                    DispatchQueue.main.async {
                        handleBlock()
                    }
                }
                self.GCDTimer?.resume()
                self.GCDTimer?.schedule(deadline: DispatchTime.now(), repeating: timeInterval)
            }else{
                self.stopOrResumeGCDTimer(isStop: false)
            }
            
            
            
        }
        /// 暂停或者重启GCDTimer
        func stopOrResumeGCDTimer(isStop: Bool){
            guard self.isSuspend != isStop else {
                return
            }
            isStop == true ? self.GCDTimer?.suspend() : self.GCDTimer?.resume()
            self.isSuspend = isStop
        }
        /// 销毁GCD定时器
        func invaliGCDTimer() {
            if self.isSuspend == true {
                self.GCDTimer?.resume()
            }
            self.GCDTimer?.cancel() //销毁前不能为suspend(挂起状态)
            self.GCDTimer = nil
        }
    }
    

      

    2.使用

        //倒计时
        var countdownTimer = ZDTimerTool()
    

      

    适当地方开启定时器       
     countdownTimer.DispatchTimer(timeInterval: 1) { [weak self] in
                self?.handTimer()
            }
    

      

        func handTimer() {
            if self.remainingSeconds == 0{
                self.remainingSeconds = 60
                self.sendButton.setTitle("重新发送", for: .normal)
                self.sendButton.backgroundColor = UIColor.init(hexColor: "FF8E00")
                self.sendButton.isEnabled = true
                self.countdownTimer.stopOrResumeGCDTimer(isStop: true)
                
            }else{
                sendButton.setTitle("(remainingSeconds)秒后重新获取", for: .normal)
                self.sendButton.backgroundColor = UIColor.gray
                sendButton.isEnabled = false
            }
            self.remainingSeconds -= 1
        }
    

      

  • 相关阅读:
    Xcode4.2 本地化 总结
    Android中后台线程如何与UI线程交互
    Android中后台线程如何与UI线程交互
    如何解决iOS内存错误
    如何解决iOS内存错误
    genymotion 和genymotion eclipse 插件安装 !
    genymotion 和genymotion eclipse 插件安装 !
    CoderForces 518C Anya and Smartphone (模拟)
    CodeForces 518B Tanya and Postcard (题意,水题)
    CodeForces 513A Game (水题,博弈)
  • 原文地址:https://www.cnblogs.com/qingzZ/p/10003279.html
Copyright © 2020-2023  润新知