class WTGCDTimer: NSObject {
static var codeTimer: DispatchSourceTimer?
class func start(_ timeInterval: TimeInterval = 1,_ totolTimeInterval: TimeInterval = Double(MAXFLOAT), animation: @escaping (_ isFinish: Bool)->()){
var timeCount = totolTimeInterval
WTGCDTimer.codeTimer = DispatchSource.makeTimerSource(queue: DispatchQueue.global())
WTGCDTimer.codeTimer?.schedule(deadline: .now(), repeating: timeInterval)
var isFinish = false
WTGCDTimer.codeTimer?.setEventHandler(handler: {
timeCount = timeCount - timeInterval
if timeCount == 0 {
WTGCDTimer.codeTimer?.cancel()
isFinish = true
}
DispatchQueue.main.async {
animation(isFinish)
}
})
WTGCDTimer.codeTimer?.resume()
}
class func stop() {
WTGCDTimer.codeTimer?.cancel()
}
}
网友评论