2017-02-10 149 views
0

我的UI使用UILabel的動畫來表示特定的狀態。我通過在NSUserDefaults standardUserDefaults中使用一個鍵來打開和關閉該狀態。在模擬主頁按鈕並點擊應用程序後,該應用程序變爲活動狀態時,動畫將正確重啓(在模擬器中)。但是,如果我模擬鎖定按鈕,然後單擊主頁,它不會正確重新啓動。這兩個事件都顯示在控制檯和方法中 - 在兩種情況下調用(void)startFlashingButton。我無法弄清楚它爲什麼在一種情況下起作用,而不是在另一種情況下起作用。我將不勝感激任何幫助。ios動畫無法重新啓動

編輯2017年2月14日:我在其他有關UINotification的董事會上看過幾篇文章。顯然很難開始工作。當通知進入應用程序委託時,我的動畫被觸發。我想建立一種「請勿打擾」系統,以便在特定時間使通知無效。我的理解是,這不能通過UINotification實現自動化,因爲除非應用程序位於前臺和中心,否則應用程序不會看到通知,或者它位於後臺並且用戶點擊其中一個操作按鈕。如果應用程序處於後臺並且用戶忽略警報,則該應用程序不會被觸發,因此無法知道是否已達到請勿打擾時間。另外,你不能用NSTimer可靠地做到這一點,因爲當應用程序暫停時,定時器將不會被識別,這將在後臺進入後不久。所以,簡單地說,我有一個比模擬沒有運行更大的問題。但是,感謝你們所有人的回覆。

這裏是startFlashingButton方法:

-(void) startFlashingButton 
{ 
    NSUserDefaults *storage = [NSUserDefaults standardUserDefaults]; 
    if([storage boolForKey:@"animation started"] == YES){ 
    // Fade out the view right away 
    [UIView animateWithDuration:2.0 
          delay: 0.0 
         options: (UIViewAnimationOptionCurveEaseIn | UIViewAnimationOptionRepeat | UIViewAnimationOptionAllowUserInteraction) 
        animations:^{ 
         self.timerLabel.alpha = 0.0; 
        } 
        completion:^(BOOL finished){ 
         // Wait one second and then fade in the view 
         [UIView animateWithDuration:1.0 
               delay: 2.0 
              options:UIViewAnimationOptionCurveEaseOut 
              animations:^{ 
               self.timerLabel.alpha = 1.0; 
              } 
              completion:nil]; 
        }]; 
    } 
    [storage synchronize]; 
} 

在-viewDidLoad,我設置的應用程序時,它變得活躍如下通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startFlashingButton) name:UIApplicationDidBecomeActiveNotification object:nil]; 

在應用程序的委託,我synchonize用戶默認當應用程序進入後臺,如下所示:

- (void)applicationDidEnterBackground:(UIApplication *)application { 
// 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. 
NSLog(@"application did enter background"); 
NSUserDefaults *storage = [NSUserDefaults standardUserDefaults]; 
[storage synchronize]; 
+0

讓我們調試。應用程序回到最前面時會調用「startFlashingButton」嗎?如果是,那麼'animateWithDuration'部分是否運行? – matt

+0

在你的'info.plist'中'Application不在'後臺運行'是否設置爲'NO',你是否調試過'applicationDidEnterBackground:'它被調用? –

+0

是的,調用applicationDidEnterBackground。不,info.plist沒有設置爲應用程序在後臺運行,因爲蘋果只會允許這幾個類別,我的應用程序可能不適用。 –

回答

0

由於Apple的文檔上background transition cycle塔季翁狀態

當用戶按下主頁按鈕,按下睡眠/喚醒按鈕,或者系統將啓動另一個應用中,前臺應用程序轉換到活動狀態,然後到後臺狀態。這些轉換導致調用應用程序委託的applicationWillResignActive:和applicationDidEnterBackground:方法。

因此,當您鎖定然後按下主頁按鈕時,該應用程序可能會退出而處於活動狀態,但不會處於後臺。

嘗試建立這個輔助方法,並調用它在這兩個applicationWillResignActive:applicationDidEnterBackground:

-(void)saveUserDefaults { 
    NSLog(@"application did enter background"); 
    NSUserDefaults *storage = [NSUserDefaults standardUserDefaults]; 
    [storage synchronize]; 
} 

,確保Application does not run in backgroundinfo.plist設置爲NO否則你的應用程序會去suspended

+0

我在調用applicationWillResignActive並調用applicationDidEnterBackground時執行此操作。該應用程序不在後臺運行,因爲蘋果只允許這幾個類別,我的應用程序可能不會在其中之一。 –