-1
我正在做一个“测验游戏”,并试图把一个计时器给用户只有10秒来尝试回答这个问题。如果10秒钟结束,它会弹出一个警报警告,需要很长时间才能回答。但如果用户在时间用完之前触摸正确的答案,我希望计时器开始新的计数。我以各种方式尝试,仍然没有得到,也没有找到类似于我自己的问题。谢谢!触摸按钮后,计时器再次开始计数(斯威夫特)
我正在做一个“测验游戏”,并试图把一个计时器给用户只有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()