我正在开发一个游戏,我想创建一个暂停菜单.这是我的代码:
self.view?.paused = true
但NSTimer.scheduledTimerWithTimeInterval仍在运行..
for var i=0; i < rocketCount; i++ { var a:NSTimeInterval = 1 ii += a delaysShow = 2.0 + ((stimulus + interStimulus) * ii) var time3 = NSTimer.scheduledTimerWithTimeInterval(delaysShow!,target: self,selector: Selector("showRocket:"),userInfo: rocketid[i],repeats: false) }
当玩家点击暂停菜单时,我想要time3暂停计时器,玩家回到游戏时继续运行定时器,但是如何暂停NSTimer.scheduledTimerWithTimeInterval?请帮帮我.
你需要使它无效并重新创建它.您可以使用isPaused bool来跟踪状态,如果您有相同的按钮暂停和恢复计时器:
var isPaused = true var timer = NSTimer() @IBAction func pauseResume(sender: AnyObject) { if isPaused{ timer = NSTimer.scheduledTimerWithTimeInterval(0.1,selector: Selector("somAction"),userInfo: nil,repeats: true) isPaused = false } else { timer.invalidate() isPaused = true } }