WKInterfaceTimer 用作定时器开始和停止倒计时
WKInterfaceTimer used as a timer to countdown start and stop
我正在尝试创建一个计时器来倒计时 x 分钟和 y 秒。
我正在计算秒数并像这样创建 InterfaceTimer:
timer.setDate(NSDate(timeIntervalSinceNow:Double(secondsValue+1)))
timer.stop()
之后我不停地停止它并一次又一次地启动它,但是值突然下降为 "time(now) doesn't stop"。
例如:如果计时器显示:55,我启动它 3 秒然后停止它,它显示:52,我等待 10 秒然后再次启动它,它从 :42 开始。
我无法保存当前在WKInterfaceTimer 中的值,以便我可以从同一点重新开始。我尝试的一切都不起作用。有没有人使用计时器并且在停止后它保持相同的值?
是的,watchkit 计时器有点……笨拙……而且绝对不是很直观。但这只是我的意见
每次用户选择恢复计时器时,您都必须继续设置 date/timer。
请记住,您还需要一个内部 NSTimer 来跟踪事物,因为当前的 WatchKit 计时器只是用于显示而没有附加任何实际逻辑。
所以也许是这样的……不优雅。但它有效
@IBOutlet weak var WKTimer: WKInterfaceTimer! //watchkit timer that the user will see
var myTimer : NSTimer? //internal timer to keep track
var isPaused = false //flag to determine if it is paused or not
var elapsedTime : NSTimeInterval = 0.0 //time that has passed between pause/resume
var startTime = NSDate()
var duration : NSTimeInterval = 45.0 //arbitrary number. 45 seconds
override func willActivate(){
super.willActivate()
myTimer = NSTimer.scheduledTimerWithTimeInterval(duration, target: self, selector: Selector("timerDone"), userInfo: nil, repeats: false)
WKTimer.setDate(NSDate(timeIntervalSinceNow: duration ))
WKTimer.start()
}
@IBAction func pauseResumePressed() {
//timer is paused. so unpause it and resume countdown
if isPaused{
isPaused = false
myTimer = NSTimer.scheduledTimerWithTimeInterval(duration - elapsedTime, target: self, selector: Selector("timerDone"), userInfo: nil, repeats: false)
WKTimer.setDate(NSDate(timeIntervalSinceNow: duration - elapsedTime))
WKTimer.start()
startTime = NSDate()
pauseResumeButton.setTitle("Pause")
}
//pause the timer
else{
isPaused = true
//get how much time has passed before they paused it
let paused = NSDate()
elapsedTime += paused.timeIntervalSinceDate(startTime)
//stop watchkit timer on the screen
WKTimer.stop()
//stop the ticking of the internal timer
myTimer!.invalidate()
//do whatever UI changes you need to
pauseResumeButton.setTitle("Resume")
}
}
func timerDone(){
//timer done counting down
}
我正在尝试创建一个计时器来倒计时 x 分钟和 y 秒。 我正在计算秒数并像这样创建 InterfaceTimer: timer.setDate(NSDate(timeIntervalSinceNow:Double(secondsValue+1))) timer.stop()
之后我不停地停止它并一次又一次地启动它,但是值突然下降为 "time(now) doesn't stop"。 例如:如果计时器显示:55,我启动它 3 秒然后停止它,它显示:52,我等待 10 秒然后再次启动它,它从 :42 开始。
我无法保存当前在WKInterfaceTimer 中的值,以便我可以从同一点重新开始。我尝试的一切都不起作用。有没有人使用计时器并且在停止后它保持相同的值?
是的,watchkit 计时器有点……笨拙……而且绝对不是很直观。但这只是我的意见
每次用户选择恢复计时器时,您都必须继续设置 date/timer。
请记住,您还需要一个内部 NSTimer 来跟踪事物,因为当前的 WatchKit 计时器只是用于显示而没有附加任何实际逻辑。
所以也许是这样的……不优雅。但它有效
@IBOutlet weak var WKTimer: WKInterfaceTimer! //watchkit timer that the user will see
var myTimer : NSTimer? //internal timer to keep track
var isPaused = false //flag to determine if it is paused or not
var elapsedTime : NSTimeInterval = 0.0 //time that has passed between pause/resume
var startTime = NSDate()
var duration : NSTimeInterval = 45.0 //arbitrary number. 45 seconds
override func willActivate(){
super.willActivate()
myTimer = NSTimer.scheduledTimerWithTimeInterval(duration, target: self, selector: Selector("timerDone"), userInfo: nil, repeats: false)
WKTimer.setDate(NSDate(timeIntervalSinceNow: duration ))
WKTimer.start()
}
@IBAction func pauseResumePressed() {
//timer is paused. so unpause it and resume countdown
if isPaused{
isPaused = false
myTimer = NSTimer.scheduledTimerWithTimeInterval(duration - elapsedTime, target: self, selector: Selector("timerDone"), userInfo: nil, repeats: false)
WKTimer.setDate(NSDate(timeIntervalSinceNow: duration - elapsedTime))
WKTimer.start()
startTime = NSDate()
pauseResumeButton.setTitle("Pause")
}
//pause the timer
else{
isPaused = true
//get how much time has passed before they paused it
let paused = NSDate()
elapsedTime += paused.timeIntervalSinceDate(startTime)
//stop watchkit timer on the screen
WKTimer.stop()
//stop the ticking of the internal timer
myTimer!.invalidate()
//do whatever UI changes you need to
pauseResumeButton.setTitle("Resume")
}
}
func timerDone(){
//timer done counting down
}