Simple and Elegant Timer
中文介绍:打造一个优雅的Timer
- No retain cycle
- Decouple with RunLoop
- Support GCD queue
- Support dynamically changing interval
- Support closure syntax
//We need to maintain a strong reference to SwiftTimer. When self dealloc, timer will dealloc too.
self.timer = SwiftTimer(interval: .seconds(2)) {
print("fire")
}
self.timer.start()
timer = SwiftTimer.repeaticTimer(interval: .seconds(1)) {
print("fire")
}
timer.start()
dynamically changing interval
timer = SwiftTimer.repeaticTimer(interval: .seconds(5)) { timer in
print("doSomething")
}
timer.start() // print doSomething every 5 seconds
func speedUp(timer: SwiftTimer) {
timer.rescheduleRepeating(interval: .seconds(1))
}
speedUp(timer) // print doSomething every 1 second
The closure will be called after interval you specified. It is invalid to call again in the interval.
SwiftTimer.throttle(interval: .seconds(0.5), identifier: "refresh") {
refresh();
}
The closure will be called after interval you specified. Calling again in the interval cancels the previous call.
SwiftTimer.debounce(interval: .seconds(0.5), identifier: "search") {
search(inputText)
}
timer = SwiftCountDownTimer(interval: .fromSeconds(0.1), times: 10) { timer , leftTimes in
label.text = "\(leftTimes)"
}
timer.start()
Carthage:
github "100mango/SwiftTimer"