我希望通过点击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
}
}
答案 0 :(得分:1)
您是否尝试在timer.invalidate()
方法中调用resetButton
?