美文网首页
swift GDC定时器

swift GDC定时器

作者: 倾倒的吞天壶 | 来源:发表于2018-04-27 10:02 被阅读22次
    /// GCD定时器倒计时⏳
    ///   - timeInterval: 循环间隔时间
    ///   - repeatCount: 重复次数
    ///   - handler: 循环事件, 闭包参数: 1. timer, 2. 剩余执行次数
    public func DispatchTimer(timeInterval: Double, repeatCount:Int, handler:@escaping (DispatchSourceTimer?, Int)->())
    {
        if repeatCount <= 0 {
            return
        }
        let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)
        var count = repeatCount
        timer.schedule(wallDeadline: .now(), repeating: timeInterval)
        timer.setEventHandler(handler: {
            count -= 1
            DispatchQueue.main.async {
                handler(timer, count)
            }
            if count == 0 {
                timer.cancel()
            }
        }) 
        timer.resume()
    }
    
    /// GCD定时器循环操作
    ///   - timeInterval: 循环间隔时间
    ///   - handler: 循环事件
    public func DispatchTimer(timeInterval: Double, handler:@escaping (DispatchSourceTimer?)->())
    {
        let timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.main)
        timer.schedule(deadline: .now(), repeating: timeInterval)
        timer.setEventHandler {
            DispatchQueue.main.async {
                handler(timer)
            }
        }
        timer.resume()
    }
    
    /// GCD延时操作
    ///   - after: 延迟的时间
    ///   - handler: 事件
    public func DispatchAfter(after: Double, handler:@escaping ()->())
    {
        DispatchQueue.main.asyncAfter(deadline: .now() + after) { 
            handler()
        }
    }
    
    /// 使用
       override func viewDidLoad() {
            super.viewDidLoad()
    
            DispatchTimer(timeInterval: 1, repeatCount: 10) { (timer, count) in
                    print("剩余执行次数 = \(count)")
                }
    
            DispatchAfter(after: 5) { 
                print("您好")
            }
        }
    

    相关文章

      网友评论

          本文标题:swift GDC定时器

          本文链接:https://www.haomeiwen.com/subject/znfblftx.html