2015-05-01 141 views
1

我一直在快速開發一個遊戲,當主頁按鈕被意外點擊時,我想運行一個重新啓動遊戲的函數。有沒有人知道什麼代碼需要實現調用一個功能,當主頁按鈕被點擊和應用程序關閉。 (按home鍵我指的是實際的硬件按鈕)在主頁按鈕點擊時調用一個函數

+0

你要當你的用戶離開應用程序,然後知道嗎? – ABakerSmith

+0

這個應用程序是如何關閉的? – BSMP

+0

不,不管它如何實現,只要應用程序關閉 –

回答

5

您可以使用以下方法之一在你的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() 
} 
+0

當用戶按下主頁按鈕時會調用其中的哪一個? – DrZ214

+1

他們都應該被調用。查看應用程序的執行狀態部分https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html –

相關問題