2010-11-09 80 views
5

我遇到了iPhone動畫塊的一個奇怪問題。 此代碼:UIView animateWithDuration:延遲:奇怪的工作

[UIView animateWithDuration:2 delay: 0 options: 0 animations: 
^(void) { [controller setBackgroundColor: [UIColor blueColor]]; } 
completion: nil]; 

[UIView animateWithDuration:2 delay: 2 options: 0 animations: 
^(void) { [controller setBackgroundColor: [UIColor redColor]]; } 
completion: nil]; 

立即將背景設置爲紅色,而不經過藍色狀態。 即使是在這個代碼同樣的事情:

[UIView animateWithDuration:2 delay: 0 options: 0 animations: 
^(void) { [controller setBackgroundColor: [UIColor blueColor]]; } 
completion: 

^(BOOL wrwg) { 
    [UIView animateWithDuration:2 delay: 2 options: 0 animations: 
    ^(void) { [controller setBackgroundColor: [UIColor redColor]]; } 
    completion: nil]; 
}]; 

也就是說,當我嘗試後的第一個已完成運行第二個動畫。 什麼問題?

+0

什麼類型的對象是控制器? UIView,UILabel,其他? – Anna 2010-11-10 02:52:40

回答

3

我只是有這個問題,我通常有它。有兩個原因,儘管我還沒有測試。

原因之一,我忘了將界面生成器連接到動畫視圖,因此該塊表示「在nil上生成動畫?沒有任何操作,所以我要跳到完成塊!

另一個原因是,有些事情要做,但它不是一個可動畫的屬性(隱藏),我敢打賭,只要動畫中的變量符合它們的「to」或「結束狀態。這只是一個理論,但我敢打賭,它是一個獨立的線程,以一定的速度執行: if(currentValueOfProperty = finalValue)doCompletion()。 (赦免僞代碼)。

有人也許應該測試,雖然

5

物業backgroundColor在UIView的動畫。相反,使用視圖圖層的backgroundColor屬性:

#import <QuartzCore/QuartzCore.h> // don't forget to link this library in project settings 

[UIView animateWithDuration:2.0 
         delay:0.0 
        options:UIViewAnimatingOptionBeginFromCurrentState 
       animations:^(void){ 
        controller.layer.backgroundColor = [UIColor blueColor].CGColor; 
       } 
       completion:NULL]; 

P.S.正如在以前的帖子中指出的那樣,請確保您的controllerUIView繼承。如果它是從UIViewController繼承的,請改爲使用controller.view