Search code examples
iosswiftnsdatewatchkit

WKInterfaceTimer used as a timer to countdown start and 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()

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.

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?


Solution

  • 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.

    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
          }