You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

76 lines
2.1 KiB

//
// GCDTimer.swift
// FireBoltt
//
// Created by lemo on 2018/5/20.
// Copyright © 2020 ecell. All rights reserved.
//
import UIKit
class GCDTimer: NSObject {
///
static let shared = GCDTimer()
/// oc
@objc open class func ocShareInstance() -> GCDTimer {
return shared
}
lazy var timerContainer = [String: DispatchSourceTimer]()
/// GCD
///
/// - Parameters:
/// - name:
/// - timeInterval:
/// - queue:
/// - repeats:
/// - immediately:
/// - action:
@objc
func scheduledDispatchTimer(WithTimerName name: String?, timeInterval: Double, queue: DispatchQueue, repeats: Bool, immediately: Bool, action: @escaping ActionClosure) {
if name == nil {
return
}
var timer = timerContainer[name!]
if timer == nil {
timer = DispatchSource.makeTimerSource(flags: [], queue: queue)
timer?.resume()
timerContainer[name!] = timer
}
// 0.1
timer?.schedule(deadline: immediately ? .now() : .now() + timeInterval, repeating: timeInterval, leeway: DispatchTimeInterval.milliseconds(100))
timer?.setEventHandler(handler: { [weak self] in
action()
if repeats == false {
self?.cancleTimer(WithTimerName: name)
}
})
}
///
///
/// - Parameter name:
@objc
func cancleTimer(WithTimerName name: String?) {
let timer = timerContainer[name!]
if timer == nil {
return
}
timerContainer.removeValue(forKey: name!)
timer?.cancel()
}
///
///
/// - Parameter name:
/// - Returns:
@objc
func isExistTimer(WithTimerName name: String?) -> Bool {
if timerContainer[name!] != nil {
return true
}
return false
}
}