WKInterfaceTimer用作倒计时开始和停止的计时器 [英] WKInterfaceTimer used as a timer to countdown start and stop

查看:89
本文介绍了WKInterfaceTimer用作倒计时开始和停止的计时器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个计时器来倒数x分钟和y秒。
我正在计算秒数并创建InterfaceTimer,如下所示:
timer.setDate(NSDate(timeIntervalSinceNow:Double(secondsValue + 1)))
timer.stop()

I am trying to create a timer to countdown x minutes and y seconds. I am computing the number of seconds and creating the InterfaceTimer like this: timer.setDate(NSDate(timeIntervalSinceNow:Double(secondsValue+1))) timer.stop()

之后我一直停下来并一次又一次地启动它,但是随着时间(现在)不停止,值突然减少。
例如:如果计时器显示:55,我启动它持续3秒并停止它,它显示:52,我等待10秒然后再次启动它,它从:42开始。

after that I keep stoping it and starting it again and again, but the values are suddenly decreasing as "time(now) doesn't stop". Eg: if the timer shows :55, I start it for 3sec and stop it, it shows :52, I wait 10seconds and then start it again, it starts from :42.

我无法保存当前在WKInterfaceTimer中的值,因此我可以从同一点重新开始。我试过的一切都行不通。有没有人使用计时器,它停止后保持相同的值?

I can not save the value currently in the WKInterfaceTimer, so that I could start again from the same point. Everything I tried doesn't work. Did anyone work with the timer and it stayed at the same value after stopping it?

推荐答案

是的,Watchkit计时器有点......尴尬......绝对不是很直观。但这只是我的意见

Yes the watchkit timer is a bit...awkward...and definitely not very intuitive. But that's just my opinion

每次用户选择恢复计时器时,你都必须继续设置日期/计时器。

You'll have to keep setting the date/timer each time the user chooses to resume the timer.

请记住,你还需要一个内部的NSTimer来跟踪事物,因为当前的WatchKit定时器只是为了显示而没有附加任何真正的逻辑。

Remember, you'll also need an internal NSTimer to keep track of things since the current WatchKit timer is simply for display without having any real logic attached to it.

所以也许是这样的......它不优雅。但它的工作原理

So maybe something like this...It's not elegant. But it works

     @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
      }

这篇关于WKInterfaceTimer用作倒计时开始和停止的计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆