如何在viewDidDisappear中等待动画完成?

时间:2012-01-30 21:56:09

标签: iphone ios cocoa-touch

我希望在让UIViewController消失之前使用动画隐藏导航栏。因此我实施了以下内容:

-(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将继续执行并返回,整个视图将在动画结束前消失。如何在动画完成之前停止该方法的返回(“动画完成”的打印)。

1 个答案:

答案 0 :(得分: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

分别