当应用程序转到后台时暂停NSTimer

时间:2016-02-24 01:33:21

标签: ios swift sprite-kit nstimer

我在我的GameScene中的touchesBegan函数中有以下内容,以便定时器仅在用户触摸某个精灵时启动:

let start = SKAction.runBlock({
    NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "printDuration:", userInfo: NSDate(), repeats: true)
})

tapToStartNode.runAction(start)

以及以下功能:

func printDuration(timer: NSTimer) {
    if self.view?.paused == false {
        guard let userInfo = timer.userInfo else {
            return
        }
        guard let startDate = userInfo as? NSDate else {
            return
        }
        let duration = NSDate().timeIntervalSinceDate(startDate)
        currentTime = NSDate().timeIntervalSinceDate(startDate)
        currentTimeValueLabel.text = "\(NSString(format:"%3.2f", duration))"
    }
}

但是,当applicationDidEnterBackground或applicationWillResignActive时,计时器没有暂停,并且当应用程序处于backgorund时继续运行。我如何让它停止?

0 个答案:

没有答案
相关问题