This is a simple class to provide a Swift Timer with the following features: start, stop, pause, resume.
To run the example project, clone the repo, and run pod install from the Example directory first.
PGMTimer is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "PGMTimer"Carthage is another dependency management tool written in Swift.
Add the following line to your Cartfile:
github "pablogm/PGMTimer"
Init timer:
timer = Timer(timerEnd: 10.0, timerWillStart: {
print("Timer started.")
}, timerDidFire: { [weak self] time in
self?.timerLabel.text = time
}, timerDidPause: {
print("Timer paused")
}, timerWillResume: {
print("Timer resumed")
}, timerDidStop: {
print("Timer stopped")
}, timerDidEnd: { [weak self] time in
self?.timerLabel.text = time
print("Timer End")
})Perform actions:
@IBAction func startTimer(sender: UIButton) {
if timer.state == .TimerStateUnkown || timer.state == .TimerStateStopped || timer.state == .TimerStateEnded {
timer.start()
}
else {
print("Can not start")
}
}
@IBAction func pauseTimer(sender: UIButton) {
if timer.state == .TimerStateRunning {
timer.pause()
}
else {
print("Can not pause")
}
}
@IBAction func resumeTimer(sender: UIButton) {
if timer.state == .TimerStatePaused {
timer.resume()
}
else {
print("Can not resume")
}
}
@IBAction func stopTimer(sender: UIButton) {
if timer.state == .TimerStateRunning {
timer.stop()
}
else {
print("Can not stop")
}
}Supports iOS8 and above. XCode 7.0 is required to build the latest code written in Swift 2.0
Pablo GM, invanzert@gmail.com
PGMTimer is available under the MIT license. See the LICENSE file for more info.