【问题标题】:Stop Game App if Home Button clicked iOS [closed]如果主页按钮单击 iOS,则停止游戏应用程序 [关闭]
【发布时间】:2015-08-09 20:24:01
【问题描述】:

我不知道根据 Apple 的规则是否有必要实现,当用户在游戏中时,如果他们点击主页按钮,游戏会暂停,而当他们在游戏中点击返回时,游戏会取消暂停。

我是否必须单击情节提要上的某些内容才能显示主页按钮?我不知道。

由于情节提要上没有主页按钮,所以当用户单击主页按钮退出应用程序时,我如何编写代码,游戏会暂停。当他们再次返回时,游戏取消暂停。

【问题讨论】:

  • Home 按钮是 iOS 设备上的物理按钮。
  • 阅读有关应用程序生命周期的文档并查看UIApplicationDelegate 中的委托方法。

标签: ios storyboard home-button


【解决方案1】:

如果单击主页按钮,则会调用您的 App Delegate 的方法 applicationDidEnterBackground:。因此,您可以随心所欲地做出回应。

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

【讨论】:

    【解决方案2】:

    有两种实现方式:

    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:使用UIApplicationDelegatedelegate 方法

    - (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.
    }
    

    【讨论】:

      【解决方案3】:

      如果您正在制作一个简单的游戏,则无需执行任何操作即可使其暂停。内存管理机制会处理它。

      但是,如果您制作的游戏需要某些功能(例如互联网连接),您将不得不处理如何处理您的任务。例如,请查看苹果的UIApplicationDelegate 参考页面。查看“管理状态转换”部分以更好地理解。

      【讨论】:

        【解决方案4】:

        在iOS模拟器中,你可以用CMD+SHIFT+H快捷键“按下home键”。

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

        applicationWillResignActive:(离开前台状态时调用。)
        applicationWillEnterForeground:(从后台状态转出时调用。)

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

        UIApplicationWillEnterForegroundNotification
        UIApplicationWillResignActiveNotification

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-23
          • 2013-02-20
          • 1970-01-01
          • 1970-01-01
          • 2015-03-03
          相关资源
          最近更新 更多