[英]How to wait for an animation to finish in viewDidDisappear?
我想在让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将继续执行并返回,整个视图将在动画结束前消失。 如何在动画完成之前(在“动画完成”打印的情况下)停止方法返回。
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
分别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.