Ios ApplicationIDBecomeActive-Swift 2.0中的调用函数

Ios ApplicationIDBecomeActive-Swift 2.0中的调用函数,ios,swift,Ios,Swift,所以,我的gamesene.swift文件中有一个func,我想在每次游戏激活时调用它。目前,我正在GameSecene.swift文件中的didMoveToView中运行此功能,但它仅在应用程序完全关闭后启动时运行,我希望在每次游戏激活时都执行此功能。(用户点击主页按钮,然后重新打开应用程序) 我假设在AppDelegate.swift文件中的applicationIDbecomeactive中运行func会起作用,但我不确定如何执行此操作,也不确定是否确实可行。任何帮助都将不胜感激 您可以在

所以,我的gamesene.swift文件中有一个func,我想在每次游戏激活时调用它。目前,我正在GameSecene.swift文件中的didMoveToView中运行此功能,但它仅在应用程序完全关闭后启动时运行,我希望在每次游戏激活时都执行此功能。(用户点击主页按钮,然后重新打开应用程序)


我假设在AppDelegate.swift文件中的applicationIDbecomeactive中运行func会起作用,但我不确定如何执行此操作,也不确定是否确实可行。任何帮助都将不胜感激

您可以在GameSecene.swift中为
UIApplicationIDBecMeactiveNotification
添加一个观察者,并调用您的函数。e、 g

NSNotificationCenter.defaultCenter().addObserver(self, 
selector: #selector(GameScene.yourfunction,
name:UIApplicationDidBecomeActiveNotification, object: nil)

在这种情况下,请在AppDelegate的ApplicationIDBecMeactive中发布NSNotification

let nc = NSNotificationCenter.defaultCenter()
nc.postNotificationName("didBecomeActive", object: nil)
然后,将侦听器连接到GameSecene.swift文件

let nc = NSNotificationCenter.defaultCenter()
nc.addObserver(self, selector: "didBecomeActive", name: "didBecomeActive", object: nil)
最后,实现选择器“didBecomeActive”


以下是基于@firstinq答案的SWIFT 5更新

NotificationCenter.default.addObserver(self, selector: #selector(MainViewController.appDidBecomeActive), name:UIApplication.didBecomeActiveNotification, object: nil)

这可能有助于提到,这个函数只是改变了一些SKSpriteNode纹理,如。。。backgroundSprite.texture=SKTexture(ImageName:“tempBG”),所以如果有其他方法在应用程序激活时更改纹理,我洗耳恭听。谢谢#选择器现在是编写选择器的合适方法。就是这样!谢谢SWIFT 3更新:
NotificationCenter.default.addObserver(self,selector:#selector(ViewController.checkBundleSettings(notification:)),name:NSNotification.name.uiapplicationidbecomeactive,object:nil)
方法是:
func checkBundleSettings(notification:notification){…your method}
NotificationCenter.default.addObserver(self, selector: #selector(MainViewController.appDidBecomeActive), name:UIApplication.didBecomeActiveNotification, object: nil)