如何防止SpriteKit游戏在打开通知中心时崩溃

时间:2018-01-03 16:08:16

标签: ios notifications sprite-kit swift4 control-center

我在使用SpriteKit游戏时遇到了一个奇怪的问题。当用户打开通知中心或控制中心时,应用程序崩溃。我想要暂停worldNode图层并在调用applicationWillResignActive时显示menuNode图层,这在按下主页按钮时工作正常。我已尝试在AppDelegate函数applicationWillResignActive内暂停我的游戏,如果游戏已启动,我会在调用applicationDidBecomeActive时尝试暂停。我尝试在NotificationCenter内使用ViewController方法同时使用Home Button / Home Gesture。我该怎么处理?或者这只是iOS 11中的一个错误?

我在viewDidLoad

ViewController方法中有这个
NotificationCenter.default.addObserver(self, selector:  #selector(appWillResignActive), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(appDidBecomeActive), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)

然后是每个选择器 如果在appDidBecomeActiveappWillResignActive

时尝试显示菜单,则会崩溃
@objc func appDidBecomeActive() {
    print("appDidBecomeActive")

    if GameScene.sharedInstance != nil {
        print("presentingMenu")
        GameScene.sharedInstance.presentMenuNode()
    }
}

@objc func appWillResignActive() {
    print("appWillResignActive")

    if GameScene.sharedInstance != nil {
        print("presentingMenu")
        GameScene.sharedInstance.presentMenuNode()
    }

}

我觉得我可能试图以错误的方式处理这个问题,但我不明白为什么当Home按钮/ Home手势被触发时它会起作用?

编辑: 经过更多测试后,我发现在iOS 10设备上运行时,一切都按预期工作。然而,当我运行苹果从他们的示例代码提供的DemoBots应用程序时,它不会在iOS 11上崩溃并基本上做我想做的事情,所以必须有一个更好的方法来处理游戏状态的转换,任何感谢投入。

0 个答案:

没有答案