-5

The below link is my code for StopWatch app. I have done everything is perfect. But when i press on PAUSE button timer is stopping, to restart again I am going to click START button. But at that time timer runs from starting not from where the pause button stops. Please advice me to resolve this issue. import UIKit

@IBAction func Play(sender: AnyObject) {

    timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("increment"), userInfo: nil, repeats: true)
    startTime = NSDate.timeIntervalSinceReferenceDate()

   }

@IBAction func Pause(sender: AnyObject) {
   timer.invalidate()


}

@IBAction func Stop(sender: AnyObject) {

    timer.invalidate()
    time = 0

    resultLabel.text = "\(0):\(0):\(0):\(0)"

}
Sri
  • 1
  • 1

2 Answers2

1

To Start:

timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateView"), userInfo: nil, repeats: true)

To Resume:

timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("updateView"), userInfo: nil, repeats: true)

To Pause:

timer.invalidate
Mohamad Bachir Sidani
  • 2,077
  • 1
  • 11
  • 17
0

You should only be calling

startTime = NSDate.timeIntervalSinceReferenceDate()

When you start it originally. If you call that when you resume you will override the start time and it will be as if you are starting over.

Wyetro
  • 8,439
  • 9
  • 46
  • 64