简体   繁体   English

快速按下主页按钮时暂停计时器

[英]Pausing timers when home button is pressed swift

I've got a bunch of timers in my GameScene.swift that call functions. 我的GameScene.swift中有一堆计时器来调用函数。 How can I pause these timers when the user taps or double taps the home button, gets a text message etc? 当用户点击或双击主页按钮,收到短信等时,如何暂停这些计时器? Right now when the home button is double tapped while the game is running the score which is based on a timer keeps going up and enemies are still spawned. 现在,当游戏运行时,在两次按下主屏幕按钮时,基于计时器的得分不断上升,并且仍然产生敌人。

    //Spawn timer for enemy blocks
    var timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("spawnEnemies"), userInfo: nil, repeats: true)

    //Timer for keeping score
    var scoretimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("scoreCounter"), userInfo: nil, repeats: true)

Any help would be greatly appreciated! 任何帮助将不胜感激!

EDIT: I added these two lines to my GameViewController.swift 编辑:我将这两行添加到我的GameViewController.swift

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("pauseTimers:"), name:UIApplicationWillResignActiveNotification, object: nil)

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("startTimers:"), name:UIApplicationDidBecomeActiveNotification, object: nil)

And then these two functions: 然后这两个功能:

func pauseTimers(notification : NSNotification) {
    println("Observer method called")
    timer.invalidate()
    scoretimer.invalidate()
}

func startTimers(notification : NSNotification) {
    println("Observer method called")
    timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("spawnEnemies"), userInfo: nil, repeats: true)
    scoretimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("scoreCounter"), userInfo: nil, repeats: true)
}

However, I am getting an error because when I recreate the timers in startTimers, their selectors are functions in my GameScene.swift, not GameViewController.swift. 但是,出现错误是因为在startTimers中重新创建计时器时,它们的选择器是我的GameScene.swift中的函数,而不是GameViewController.swift中的函数。 How can I fix this? 我怎样才能解决这个问题?

You can stop a timer by calling invalidate() on it. 您可以通过在计时器上调用invalidate()来停止计时器。 Though an invalidated timer will never run again, you need to re-create it later on if you want it to fire again. 尽管无效的计时器将永远不会再次运行,但是如果您希望它再次触发,则需要稍后重新创建它。

To detect when your app goes in background or is interrupted and also when it comes back to foreground, you can listen for these notifications: 要检测您的应用何时进入后台或被中断,以及何时回到前台,您可以侦听以下通知:

  • UIApplicationDidBecomeActiveNotification
  • UIApplicationWillResignActiveNotification
  • UIApplicationDidEnterBackgroundNotification
  • UIApplicationWillEnterForegroundNotification

An app "resigns active" when it is covered, eg because an incoming call is signaled or because the notification center/control center is opened. 当某个应用程序被覆盖时,该应用程序“退出活动状态”,例如,因为有来电提示或通知中心/控制中心已打开。 An app goes into background, when another app goes into foreground or when the home screen is shown (which is technically also an app). 当另一个应用程序进入前台或显示主屏幕时,一个应用程序进入后台(从技术上讲,这也是一个应用程序)。

在此处输入图片说明

@Update @更新

The problem is that the target of the timers is self and when you are in GameViewController , then self is the GameViewController instance. 问题在于计时器的目标self ,当您在GameViewController ,那么selfGameViewController实例。 You either need to recreate the timers within GameScene or you need to pass a reference to your GameScene instance instead of self as the target . 您要么需要在GameScene重新创建计时器,要么需要传递对GameScene实例的引用,而不是将self作为目标 The selector is the method that is called and the target is the object on that the method is called and self is always the current object. 选择器是被调用的方法, 目标是被调用该方法的对象,而self始终是当前对象。

BTW it's possible to listen to these notifications in any class and also in multiple classes at the same time. 顺便说一句,可以在任何班级或多个班级同时收听这些通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM