// // MCGCDTimer.swift // GCD Timer // // Created by Enhart on 2017/6/15. // Copyright © 2017年 Enhart. All rights reserved. // import UIKit typealias ActionBlock = () -> () class MCGCDTimer { /// 单例 static let shared = MCGCDTimer() private init(){} lazy var timerContainer = [String: DispatchSourceTimer]() /// GCD定时器 /// /// - Parameters: /// - name: 定时器名字 /// - timeInterval: 时间间隔 /// - queue: 队列 /// - repeats: 是否重复 /// - action: 执行任务的闭包 func scheduledDispatchTimer(WithTimerName name: String, timeInterval: Double, queue: DispatchQueue, repeats: Bool, action: @escaping ActionBlock) { if isExistTimer(WithTimerName: name) { self.cancleTimer(WithTimerName: name) } var timer = timerContainer[name] if timer == nil { timer = DispatchSource.makeTimerSource(flags: [], queue: queue) timer?.resume() timerContainer[name] = timer } //精度0.1秒 timer?.schedule(deadline: .now(), repeating: timeInterval, leeway: DispatchTimeInterval.milliseconds(100)) timer?.setEventHandler(handler: { [weak self] in action() if repeats == false { self?.cancleTimer(WithTimerName: name) } }) } /// 取消定时器 /// /// - Parameter name: 定时器名字 func cancleTimer(WithTimerName name: String) { let timer = timerContainer[name] if timer == nil { return } timerContainer.removeValue(forKey: name) timer?.cancel() } /// 检查定时器是否已存在 /// /// - Parameter name: 定时器名字 /// - Returns: 是否已经存在定时器 func isExistTimer(WithTimerName name: String?) -> Bool { if timerContainer[name!] != nil { return true } return false } } extension MCGCDTimer { /// 取消所有定时器 func cancleAll() { let values = timerContainer.values values.forEach { $0.cancel() } timerContainer.removeAll() } }