如果主页按钮单击iOS,则停止游戏应用程序

时间:2015-05-27 16:06:32

标签: ios storyboard home-button

我不知道Apple的规则是否有必要在用户处于游戏中间时执行该操作,如果他们点击主页按钮则游戏会暂停以及何时停止点击回到游戏中,游戏取消暂停。

我是否必须点击故事板上的某些内容才能显示主页按钮?我不知道。

由于故事板上没有主页按钮,因此如何在用户单击主页按钮退出应用程序时进行编码,游戏暂停。当他们再次返回时,游戏取消暂停。

4 个答案:

答案 0 :(得分:2)

如果单击主页按钮,则会调用App Delegate方法applicationDidEnterBackground:。因此,您可以根据需要做出回应。

但是,您可能不需要在此处执行任何特殊操作,因为当您的应用进入后台时,已暂停 - 它会停止运行。因此,从某种意义上说,它会自动暂停。例如,定时器暂停。 (请注意,此功能,即定时器的自动暂停,在iOS 8模拟器中已被破坏;但它可在设备上正常工作。)

答案 1 :(得分:0)

如果你正在制作一个简单的游戏,你不必为了暂停而实施任何东西。内存管理机制将负责处理它。

但是,如果您制作的游戏需要某些功能,例如互联网连接,则必须处理如何处理您的任务。例如,请查看Apple的UIApplicationDelegate参考页面。查看"管理州过渡"部分要有更好的理解。

答案 2 :(得分:0)

在iOS模拟器中,您可以使用 CMD + SHIFT + H 快捷方式“按主页按钮”。

您可以在UIApplicationDelegate实现中覆盖以下方法:

  

applicationWillResignActive :(离开前景状态时调用。)
  applicationWillEnterForeground :(在转换出背景状态时调用。)

或者您可以使用NSNotificationCenter和等效通知:

  

UIApplicationWillEnterForegroundNotification
  UIApplicationWillResignActiveNotification

当iOS将您的应用程序置于后台时,它基本上已暂停,因为它已不再运行(除了音乐播放器,GPS消费者等之外的某些例外)。但你的渲染循环没有做任何事情。但是,您的应用程序可能会在此状态下被杀死,因此您可能希望执行一些簿记以保持应用程序状态一致。暂停您的应用程序,保存用户进度等,然后在您回来时恢复/重新启动并加载该信息不会有什么坏处。

答案 3 :(得分:0)

实现它有两种方法:

1:使用NSNotificationCenter

- (void)setupBackgroundNotification {
    // the same to applicationWillEnterForeground:
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(open)
                                                 name: UIApplicationWillEnterForegroundNotification
                                               object:nil];

    //// the same to applicationWillResignActive
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(close)
                                                 name: UIApplicationWillResignActiveNotification
                                               object:nil];
}


- (void)open {
    NSLog(@"the same to applicationWillEnterForeground");
}

- (void)close {
    NSLog(@"the same to applicationWillResignActive");
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:applicationWillEnterForeground object:Nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:applicationWillResignActive object:Nil];
}

2:使用UIApplicationDelegate委托方法

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}