启动通知中心/控制中心时暂停 iOS 游戏 [英] Pausing iOS game when Notification Center/Control Center is launched

查看:100
本文介绍了启动通知中心/控制中心时暂停 iOS 游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我假设我的应用程序在发生此类事件时通过 NSNotificationCenter 发送通知.有谁知道这些是什么?

I’m assuming my app is sent notifications through NSNotificationCenter when these kind of events happen. Does anyone know what these are?

推荐答案

在我正在开发的应用程序中,我也需要处理这些事件,并使用以下两个通知来做到这一点:

In the app I'm working on I needed to handle these events as well and did so using the following two notifications:

UIApplicationWillResignActiveNotification >> 当通知中心或控制中心被打开时会触发此通知.

UIApplicationWillResignActiveNotification >> This notification is fired when the Notification Center or Control Center is brought up.

UIApplicationWillEnterForegroundNotification >> 关闭通知中心或控制中心时会触发此通知.

UIApplicationWillEnterForegroundNotification >> This notification is fired when the Notification Center or Control Center is dismissed.

这些事件自然可以从 AppDelegate 处理:

These events can naturally be handled from the AppDelegate:

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Handle notification
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Handle Notification
}

尽管取决于您的应用,在需要注意这一点的视图控制器中显式侦听这些事件可能会更容易:

Though depending on your app it will likely be easier to explicitly listen for these events in the View Controller(s) that needed to be aware of this:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];

...并实现您的选择器:

... and implement your selectors:

- (void)appWillResignActive:(NSNotification *)notification
{
    // Handle notification
}

- (void)appDidBecomeActive:(NSNotification *)notification
{
    // Handle notification
}

最后,当你完成后,将自己作为观察者移除:

And lastly remove yourself as an observer when you're done:

- (void)viewWillDisappear:(BOOL)animated
{
    // Remove notifications
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];

    [super viewWillDisappear:animated];
}

这篇关于启动通知中心/控制中心时暂停 iOS 游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆