触摸按钮后计时器再次开始计时 (Swift)

Timer start counting again after a button is touched (Swift)

我正在做一个 "Quiz Game" 并试图为用户设置一个计时器,让用户只有 10 秒的时间来尝试回答问题。如果超过 10 秒,它会弹出需要很长时间才能回答的警报警告。但是如果用户在时间用完之前触摸了正确的答案,我希望计时器开始新的计数。我正在尝试各种方式,但仍然没有得到,也没有找到与我自己类似的问题。谢谢!

使用 NSTimer()。

开始计时:

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

处理计时器:

func update() {

seconds++

if seconds >= 10 {

    // alert
    let alert = UIAlertController(title: "Alert", message: "Time Over", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "O.K.", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)

    // set seconds to 0
    seconds = 0

}}

停止计时器:

timer.invalidate()