2014-01-27 71 views
0

我想要一個對象消失然後隱藏它。以下隱藏它,但我沒有看到褪色,大概是因爲它在動畫仍在完成時隱藏它。任何建議如何使代碼等待動畫完成?在隱藏對象之前等待動畫完成

[UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5]; 

    [adviceBorder setAlpha:0]; 

    [UIView commitAnimations]; 

    adviceBorder.hidden=YES; 
+0

使用給出的'animateWithDuration代碼:動畫:完成:'方法。 –

回答

2

基於塊的使用動畫和做躲在完成塊

[UIView animateWithDuration:0.5 
       animations:^{ 
        adviceBorder.alpha = 0; 
       } completion:^(BOOL finished) { 
        adviceBorder.hidden = YES; 
       }]; 
+0

輝煌 - 謝謝 – RGriffiths

+1

只需添加到此;在alpha設置爲低於0.02之後,沒有理由將其設置爲隱藏。它沒有被渲染,也沒有得到任何觸摸輸入。這樣做意味着您必須記住在將隱藏設置回NO後將alpha恢復爲1,從而增加了UI中的bug。 –

0

您可以使用塊這一點。

提交動畫後,你可以把下面

double delayInSeconds = 0.5; 
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
    //code to be executed on the main queue after delay 
    adviceBorder.hidden=YES; 
}); 
+0

避免dispatch_after ..我想在這種情況下它很好,但你創建未定義/隨機應用程序的競爭條件 –