[英]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.