-1

When I hit Pause it pauses the timer. but then when I hit start, it resets to 0 instead of continue where it left off. How can I fix this?

I've tried adding a new button for reset. that works, but now I can't get the start button to keep counting after a pause. I've been struggling with getting the resume to work.

import UIKit

class TimerViewController: UIViewController {

    @IBOutlet weak var lable: UILabel!

    @objc var startTime = TimeInterval()

    var timer = Timer()

    override func viewDidLoad(){
        super.viewDidLoad()
    }

    // Start Button
    @IBAction func start(_ sender: UIButton) {
        if (!timer.isValid) {

            let aSelector = #selector(updateTime)

        timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)
        startTime = NSDate.timeIntervalSinceReferenceDate
    }
    }
    // Pause Button
    @IBAction func pause(_ sender: UIButton)
    {
        timer.invalidate()

    }

    // Reset Button
    @IBAction func reset(_ sender: UIButton)
    {
        timer.invalidate()
        lable.text = "00:00:00"

    }

    @objc func updateTime() {

        let currentTime = NSDate.timeIntervalSinceReferenceDate

        //Find the difference between current time and start time.
            var elapsedTime: TimeInterval = currentTime - startTime

        //calculate the minutes in elapsed time.
        let minutes = UInt8(elapsedTime / 60.0)
        elapsedTime -= (TimeInterval(minutes) * 60)

        //calculate the seconds in elapsed time.
        let seconds = UInt8(elapsedTime)

        elapsedTime -= TimeInterval(seconds)

        //find out the fraction of milliseconds to be displayed.
        let fraction = UInt8(elapsedTime * 100)

        //add the leading zero for minutes, seconds and millseconds and store them as string constants
        let strMinutes = String(format: "%02d", minutes)
        let strSeconds = String(format: "%02d", seconds)
        let strFraction = String(format: "%02d", fraction)

        //concatenate minuets, seconds and milliseconds as assign it to the UILabel

        lable.text = "\(strMinutes):\(strSeconds):\(strFraction)"
    }

}
  • You are setting it to nil and garbage collecting it then trying to use it from where you left off. – mewi Aug 16 '19 at 16:47
  • Thanks Mewi, Ok, so I tried deleting the line under the pause button called "timer == nil" No change, the timer works exactly the same and the start button is still resetting. What do you think I'm doing wrong? – SigmaPiFSC Aug 16 '19 at 16:52
  • https://stackoverflow.com/a/34509335/8778930 This should help – mewi Aug 16 '19 at 16:56
  • Thanks. I read through it, but I'm not sure what to change in my code. – SigmaPiFSC Aug 16 '19 at 17:07
  • Take a look at [this answer](https://stackoverflow.com/a/50200606/1630618). When the pause button is pressed, you need to compute the elapsed time so far, save that in a property, and set a state variable to note that you are paused. When you start again, if you were paused subtract that time interval from the new start time. – vacawama Aug 16 '19 at 17:08
  • You say you’re struggling to resume after the pause but you’re not even trying to do so. The elapsed time will not magically save itself, and you are making no attempt to save it. – matt Aug 17 '19 at 13:11

1 Answers1

0

keep the startTime variable from being modified until it is reset. maybe for example like this

var isReset: Bool = true
@IBAction func start(_ sender: UIButton) {
    if (!timer.isValid) {

        let aSelector = #selector(updateTime)

        timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, selector: aSelector, userInfo: nil, repeats: true)
        if isReset {
            startTime = NSDate.timeIntervalSinceReferenceDate
            isReset = false
        }
    }
}

@IBAction func reset(_ sender: UIButton) {
    timer.invalidate()
    lable.text = "00:00:00"
    isReset = true
}

Cheers...

Fadielse
  • 381
  • 2
  • 12