mineschan / MZTimerLabel

A handy class for iOS to use UILabel as a countdown timer or stopwatch just like in Apple Clock App.
MIT License
1.57k stars 247 forks source link

SetCountdownTimer adds a few extra seconds #88

Closed escakot closed 6 years ago

escakot commented 6 years ago

Output from Xcode: 29170475-d52d-4378-a348-3c10bc13e5ca

class ViewController: UIViewController {
    @IBOutlet weak var workoutCountdownTimerLabel: UILabel!
    var timerDuration: Double {
         **Logic to return a time**
    }
    lazy var countdownTimer: MZTimerLabel = MZTimerLabel(label: self.workoutCountdownTimerLabel, andTimerType: MZTimerLabelTypeTimer)
    override func viewDidLoad() {
        super.viewDidLoad()
        countdownTimer.timeFormat = "m:ss"
        countdownTimer.delegate = self
    }
    override func viewDidAppear() {
        super.viewDidAppear()
        setTimer()
    }
    func setTimer() {
        countdownTimer.setCountDownTime(timerDuration)
        countdownTimer.start()
    }
}

extension ViewController: MZTimerLabelDelegate {
    func timerLabel(_ timerLabel: MZTimerLabel!, finshedCountDownTimerWithTime countTime: TimeInterval) {
        setTimer()
    }
    func timerLabel(_ timerLabel: MZTimerLabel!, countingTo time: TimeInterval, timertype timerType: MZTimerLabelType) {
            print(time, timerDuration)
    }
}

The first time the timer is set, it's a correct time. Any subsequent setTimers() will add about 4.5 seconds. I have 2 MZTimeLabels, one being used as a stopwatch for overall time using storyboard. 1 is used as a Countdown Timer Label. Only the countdownTimer has the delegate set.

My work around it was to create a new MZTimerLabel this:
    func setTimer() {
        countdownTimer = MZTimerLabel(label: self.workoutCountdownTimerLabel, andTimerType: MZTimerLabelTypeTimer)
        countdownTimer.timeFormat = "m:ss"
        countdownTimer.delegate = self
        countdownTimer.setCountDownTime(timerDuration)
        countdownTimer.start()
    }

UPDATE:

Using countdownTimer.reset() fixes the problem.