当主页按钮在swift中轻敲时调用一个函数

时间:2015-05-01 14:30:23

标签: ios swift xcode home-button

我一直在使用swift进行游戏,当意外点击主页按钮时,我想运行一个重启游戏的功能。有人知道在点击主页按钮并关闭应用程序时需要实现调用函数的代码。 (按主页按钮我的意思是实际的硬件按钮)

1 个答案:

答案 0 :(得分:6)

您可以在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()
}