【问题标题】:Call a function when home button tapped in swift快速点击主页按钮时调用函数
【发布时间】:2015-07-11 09:34:58
【问题描述】:

我一直在快速开发游戏,当不小心点击主页按钮时,我想运行一个重新启动游戏的功能。有谁知道当点击主页按钮并关闭应用程序时需要实现什么代码来调用函数。 (我的主页按钮是指实际的硬件按钮)

【问题讨论】:

  • 您想知道您的用户何时离开应用程序吗?
  • 应用的关闭方式重要吗?
  • 不,只要关闭应用,如何实现都无所谓
  • 是的,我想知道用户何时离开应用

标签: ios swift xcode home-button


【解决方案1】:

您可以在 AppDelegate 中使用以下方法之一

// Taken straight from the AppDelegate template

func applicationWillResignActive(application: UIApplication) {
    // 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.
}

func applicationDidEnterBackground(application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillTerminate(application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    self.saveContext()
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2013-05-08
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多