我如何重置&在不使用timer.invalidate()的情况下暂停计时器

时间:2016-10-06 00:38:16

标签: ios swift swift3

我希望通过点击UI按钮来重置和暂停计时器,但是我遇到了一些问题"重置"计时器。有人可以帮忙吗?当开始时间,重置和重新开始时,多次调用更新功能,搞乱倒计时。

@IBAction func startTimer (_ sender: AnyObject) {
var timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: Selector("update"), userInfo: nil, repeats: true)
    Reset.isHidden = false
    StartButton.isHidden = true
}

@IBAction func resetButton (_ sender: AnyObject) {
    Reset.isHidden = true
    StartButton.isHidden = false
    count = 200
    invalidate.t
}



func update()  {
    if (count > 0) {
        let minutes = String(count / 60)
        let seconds = String(count % 60)
        if (seconds == "0") {
        CountDown.text = minutes + ":" + seconds + "0"
        } else {
        CountDown.text = minutes + ":" + seconds
        }
        count -= 1
    }
}

1 个答案:

答案 0 :(得分:1)

您是否尝试在timer.invalidate()方法中调用resetButton