2012-01-30 68 views
5

我想在使UIViewController消失之前使用動畫隱藏導航欄。因此,我已經實現了以下內容:如何等待動畫在viewDidDisappear中完成?

-(void) viewWillDisappear:(BOOL) animated { 
    [UIView transitionWithView:self.view 
         duration:UINavigationControllerHideShowBarDuration 
         options:UIViewAnimationCurveEaseOut 
        animations:^{ 
     [self.navigationController setNavigationBarHidden:YES];  
    } 
        completion:^(BOOL finished){ 
        NSLog(@"animation finished"); 
    }]; 

    [super viewWillDisappear:animated]; 
} 

的問題是,viewWillDisappear將繼續執行,就回到和整個視圖會消失的動畫完成之前。如何在完成動畫之前停止返回方法(其中「動畫完成」已打印)。

+0

您不能爲此設置動畫效果。也許你正在尋找'setNavigationBarHidden:animated:'方法呢? – 2012-01-30 22:04:13

+0

我不想使用'setNavigationBarHidden:animated:',因爲它在iOS 4(向左滑動)和iOS 5(向頂部滑動)方面有不同的動畫效果。無論如何,這並不重要,因爲在viewWillDisappear塊中放置'setNavigationBarHidden:animated:'仍然意味着該方法在動畫完成之前返回。我認爲這裏可能需要兩個線程? – 2012-01-30 22:22:51

回答

2

viewWillDisappear:animated本質上是禮貌通知。它只是告訴你什麼是即將發生的。你不能阻止或延遲視圖的消失。

你最好的解決辦法是在UINavigationController創建類創建方法,如(未經測試):

- (void)pushViewControllerAfterAnimation:(UIViewController *)viewController animated:(BOOL)animated { 
    [UIView transitionWithView:viewController.view 
         duration:UINavigationControllerHideShowBarDuration 
         options:UIViewAnimationCurveEaseOut 
        animations:^{ 
         [self.navigationController setNavigationBarHidden:NO];  
        } 
        completion:^(BOOL finished){ 
         NSLog(@"animation finished"); 
         [self pushViewController:viewController animated:animated]; 
        }]; 
} 

- (void)popViewControllerAfterAnimationAnimated:(BOOL)animated { 
    [UIView transitionWithView:self.visibleViewController.view 
         duration:UINavigationControllerHideShowBarDuration 
         options:UIViewAnimationCurveEaseOut 
        animations:^{ 
         [self.navigationController setNavigationBarHidden:YES];  
        } 
        completion:^(BOOL finished){ 
         NSLog(@"animation finished"); 
         [self popViewControllerAnimated:animated]; 
        }]; 
} 

然後,您可以調用這些上,而不是

- (void)pushViewControllerAfterAnimation:(UIViewController *)viewController animated:(BOOL)animated

- (void)popViewControllerAfterAnimationAnimated:(BOOL)animated

+0

看起來不錯:),雖然我不確定當使用transitionWithView時發生了什麼:使用curveEaseInOut選項時,從未嘗試過這種方式。 – 2012-05-17 21:56:34

+0

我同意,我也不完全確定。 transitionWithView的使用:來自OP的原始代碼,但我沒有測試過。 – 2012-05-18 05:46:06