I have been working on a game in swift and when the home button is tapped accidentally I would like to run a function that restarts the game. Does anyone know what code needs to be implemented to call a function when the home button is tapped and the app is closed. (by home button I mean the actual hardware button)
我一直在使用swift进行游戏,当意外点击主页按钮时,我想运行一个重启游戏的功能。有人知道在点击主页按钮并关闭应用程序时需要实现什么代码来调用函数。 (按主页按钮我的意思是实际的硬件按钮)
1 个解决方案
#1
6
You can use one of the following methods in your AppDelegate
您可以在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()
}
#1
6
You can use one of the following methods in your AppDelegate
您可以在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()
}