如何在spritekit 中按下主页button时暂停并重新启动NSTimer.scheduledTimerWithTimeInterval?

我对swift和spritekit是新手,想知道如何检测homebutton是否被按下?

主要问题是我有NSTimer.scheduledTimerWithTimeInterval运行在didMoveToView生成多个字符,一切都很顺利,但是当我用homebutton暂停游戏并在几秒钟后重新启动游戏时,字符溢出了很多。 我认为NSTimer没有被暂停,因此,重新启动应用程序时涌入了大量的字符。 我想知道一个简单的解决scheme和示例,当按下homebutton时,我将如何暂停,并在应用程序重新启动时恢复? 我很想从你这里来!

  testEnemy = NSTimer.scheduledTimerWithTimeInterval(1.2, target: self, selector: "timerUpdate", userInfo: nil, repeats: true) 

///这是用于在didMoveToView中生成字符的项目代码的一部分

 NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("appEnterIntoBackGround:"), name:UIApplicationDidEnterBackgroundNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("appBecomeActive:"), name:UIApplicationWillEnterForegroundNotification, object: nil) //start the timer and calls the timerUpdate method every 1.0 seconds myTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "timerUpdate", userInfo: nil, repeats: true) if(skview.level == 3) { myTimer2 = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "timerUpdate", userInfo: nil, repeats: true) } testEnemy = NSTimer.scheduledTimerWithTimeInterval(1.2, target: self, selector: "timerUpdate", userInfo: nil, repeats: true) } func appEnterIntoBackGround(notification : NSNotification) { let skview = self.view as! GameSKView! print("App is in Background") testEnemy.invalidate() //remove timer here when add enter into background. if(skview.level == 3) { myTimer2.invalidate() } myTimer.invalidate() } func appBecomeActive(notification : NSNotification) { let skview = self.view as! GameSKView! // print("App is active now") //add your timer again when app enter into foreground testEnemy = NSTimer.scheduledTimerWithTimeInterval(1.2,target: self,selector: "timerUpdate",userInfo: nil,repeats: true) if(skview.level == 3) { myTimer2 = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "timerUpdate", userInfo: nil, repeats: true) } testEnemy = NSTimer.scheduledTimerWithTimeInterval(1.2, target: self, selector: "timerUpdate", userInfo: nil, repeats: true) } 

你可以使用NSNotificationCenter ,当你的应用程序进入后台或前台时会触发通知,如下面的代码所示:

 var testEnemy : NSTimer? override func didMoveToView(view: SKView) { /* Setup your scene here */ NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("appEnterIntoBackGround:"), name:UIApplicationDidEnterBackgroundNotification, object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("appBecomeActive:"), name:UIApplicationWillEnterForegroundNotification, object: nil) testEnemy = NSTimer.scheduledTimerWithTimeInterval(1.2, target: self, selector: "timerUpdate", userInfo: nil, repeats: true) } 

以下是帮手方法:

 func timerUpdate() { print("called") } func appEnterIntoBackGround(notification : NSNotification) { print("App is in Background") testEnemy!.invalidate() //remove timer here when add enter into background. } func appBecomeActive(notification : NSNotification) { print("App is active now") //add your timer again when app enter into foreground testEnemy = NSTimer.scheduledTimerWithTimeInterval(1.2,target: self,selector: "timerUpdate",userInfo: nil,repeats: true) }