这是一个简单的类,提供以下功能的 Swift 定时器:开始、停止、暂停、恢复。
要运行示例项目,首先克隆仓库,然后在 Example 目录中运行 pod install
初始化定时器
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")
})
执行操作
@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")
}
}
支持 iOS8 及以上版本。要构建用 Swift 2.0 编写的最新代码,需要 XCode 7.0
Pablo GM,[email protected]
PGMTimer 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。