Iphone 如何在VIEWDID中等待动画完成?
在让UIViewController消失之前,我想使用动画隐藏导航栏。因此,我实施了以下措施:Iphone 如何在VIEWDID中等待动画完成?,iphone,ios,cocoa-touch,Iphone,Ios,Cocoa Touch,在让UIViewController消失之前,我想使用动画隐藏导航栏。因此,我实施了以下措施: -(void) viewWillDisappear:(BOOL) animated { [UIView transitionWithView:self.view duration:UINavigationControllerHideShowBarDuration options:UIViewAnimati
-(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];
}
问题是,ViewWillEnglish将继续执行并返回,而整个视图将在动画完成之前消失。如何阻止该方法在动画完成之前返回(其中“动画已完成”已打印)。
视图将消失:动画本质上是一种礼貌性通知。它只是在发生之前告诉你即将发生的事情。实际上,您不能阻止或延迟视图的消失
最好的解决方案是在UINavigationController
上创建一个类别,该类别将创建以下方法(未测试):
然后你可以打开这些,而不是
-(void)pushViewControllerAfterAnimation:(UIViewController*)viewController动画:(BOOL)动画
及
-(void)popViewControllerAfterAnimationAnimated:(BOOL)animated
分别设置。您无法设置动画。也许您正在寻找setNavigationBarHidden:animated:
方法?我不想使用setNavigationBarHidden:animated:
,因为它在iOS 4(向左滑动)和iOS 5(向上滑动)中的动画不同。无论如何,这并不重要,因为将setNavigationBarHidden:animated:
放在viewwillbeside
块中仍然意味着该方法在动画完成之前返回。我认为这里可能需要两个线程?看起来很好:),虽然我不确定使用transitionWithView:时会发生什么情况,但使用curveEaseInOut选项时,从来没有这样尝试过。我同意,我也不完全确定。transitionWithView:的使用来自OP的原始代码,但我尚未对其进行测试。
- (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];
}];
}