import UIKit
typealias ActionBlock = () -> ()
class
MRGCDTimer: NSObject {
static
let share = MRGCDTimer()
lazy var timerContainer = [String : DispatchSourceTimer]()
func scheduledDispatchTimer(withName name:String?, timeInterval:Double, queue:DispatchQueue, repeats:Bool, action:@escaping ActionBlock ) {
if
name == nil {
return
}
var timer = timerContainer[name!]
if
timer==nil {
timer = DispatchSource.makeTimerSource(flags: [], queue: queue)
timer?.resume()
timerContainer[name!] = timer
}
timer?.schedule(deadline: .now(), repeating: timeInterval, leeway: .milliseconds(100))
timer?.setEventHandler(handler: { [weak self] in
action()
if
repeats==
false
{
self?.destoryTimer(withName: name!)
}
})
}
func destoryTimer(withName name:String?) {
let timer = timerContainer[name!]
if
timer == nil {
return
}
timerContainer.removeValue(forKey: name!)
timer?.cancel()
}
func isExistTimer(withName name:String?) -> Bool {
if
timerContainer[name!] != nil {
return
true
}
return
false
}
}