2014-03-29 60 views
2

我動畫一個UIImageView上下時通過調用其執行以下操作方法animateSettings認爲負載:問題恢復UIVIEW動畫:ios7?

- (void)animateSettings { 

    NSLog(@"Animate Settings"); 
    [UIView animateWithDuration:1.0f 
         delay:0.0f 
        options:(UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat | UIViewKeyframeAnimationOptionAllowUserInteraction | UIViewAnimationOptionBeginFromCurrentState) 
       animations:^{ 
        self.waterImageView.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2-75); 
       } 
       completion:nil]; 
} 

的問題是,當我退出程序和返回,動畫凍結。我明白這是爲什麼,但我似乎無法弄清楚如何恢復它。

我已經在viewDidLoad方法中設置了NSNotifications,以便在視圖變爲活動狀態或進入後臺時通知我。做以下我稱爲animateSettings方法或removeAnimation方法。

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

[[NSNotificationCenter defaultCenter]addObserver:self 
             selector:@selector(removeAnimation) 
              name:UIApplicationDidEnterBackgroundNotification 
              object:nil]; 

這裏是我的removeAnimation方法

- (void)removeAnimation { 
    NSLog(@"Remove Animation"); 
    [self.waterImageView.layer removeAllAnimations]; 
} 

問題:即使方法正確調用(NSLogging作品)動畫仍然凍結。

+0

當您進入後臺時,不需要調用'removeAllAnimations'。它是由系統調用的。 – matt

回答

3

第一次啓動動畫(我們稱之爲)動畫代碼說:

self.waterImageView.center = 
    CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2-75); 

現在讓我們假設你進入的背景和回報。您的動畫代碼被再次調用(我們稱之爲):

self.waterImageView.center = 
    CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2-75); 

好吧,但哪裏self.waterImageView現在,在?這已經是CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2-75) - 因爲這是你在期間的地方!

因此沒有任何反應,因爲如果在動畫屬性的值中沒有更改,則沒有動畫。

知道了這一點,解決方案是顯而易見的。當您進入背景時,或者因任何原因停止動畫時,將視圖放回到以開頭的位置。現在,當你再次開始動畫時,視圖就有了動畫的地方。

+0

謝謝馬特。但是,請記住,我正在自動反轉動畫並不斷循環。我也使用UIViewAnimationOptionBeginFromCurrentState作爲選項,但它不會恢復飛行中的動畫。也許我錯了,但如果確實如此,它應該自動反向,正確嗎? – KingPolygon

+0

您在動畫中分配的位置是視圖真正呈現的位置;動畫只是表示層正在做的事情。在飛行中恢復無法工作,因爲沒有機上動畫可以恢復 - 您將其刪除! (在任何情況下,系統都會將其刪除。) – matt

+0

另請參閱我的書中對這種動畫的討論:http://www.apeth.com/iOSBook/ch17.html#SECblockbasedviewanimation – matt