2013-08-26 59 views
1

我嘗試創建方法並從animateWithDuration返回BOOL類型。但我的對象似乎沒有在完成塊中檢測到。有人可以向我解釋,爲什麼會發生這種情況?如何從animateWithDuration返回BOOL?

+ (BOOL)showAnimationFirstContent:(UIView *)view { 
    BOOL status = NO; 

    CGRect show = [SwFirstContent rectFirstContentShow]; 

    [UIView animateWithDuration:DURATION 
          delay:DELAY 
         options:UIViewAnimationOptionBeginFromCurrentState 
        animations:^{ view.frame = show; } 
        completion:^(BOOL finished) { 
         status = YES; 
        }]; 
    return status; 
} 

感謝提前。

回答

3

要設置一個塊內的狀態值將被異步執行來處理這個布爾。意思是,你的返回語句不能保證在程序段執行後執行。要知道動畫何時完成,您需要以不同的方式聲明您的方法。

+ (void)showAnimationFirstContent:(UIView *)view completion:(void (^)(void))callbackBlock{ 

    CGRect show = [SwFirstContent rectFirstContentShow]; 

    [UIView animateWithDuration:DURATION 
          delay:DELAY 
         options:UIViewAnimationOptionBeginFromCurrentState 
        animations:^{ view.frame = show; } 
        completion:^(BOOL finished) { 
         callbackBlock(); 
        }]; 
} 

你可以調用這個方法是這樣的:

[MyClass showAnimationFirstContent:aView completion:^{ 
//this block will be executed when the animation will be finished 
    [self doWhatEverYouWant]; 
}]; 

您可能需要再多讀一些有關如何block works

希望這會有所幫助。

+1

我嘗試你的解決方案和它的工作。謝謝mamnun。 –

2

因爲塊是異步執行的,所以會發生這種情況。意思是在執行animateWithDuration方法之後,showAnimationFirstContent方法將繼續執行(並且在這種情況下返回),而不等待動畫完成(並且將布爾值更改爲YES)。

你也許應該保持這個布爾作爲動畫類的成員,並在完成塊執行的方法,當動畫完成

+0

謝謝giorashc,現在我明白爲什麼我的對象沒有在塊上檢測到。 –