呈现 2 个视图控制器,但只有一个过渡
Posted
技术标签:
【中文标题】呈现 2 个视图控制器,但只有一个过渡【英文标题】:Present 2 viewcontrollers but only one transition 【发布时间】:2016-03-04 17:39:11 【问题描述】:我想从视图控制器“A”、第二个视图控制器“B”(带导航栏)推送,它应该显示第三个模式视图控制器“C”,它显示倒计时并自行关闭,最终显示“B ”。
我希望以模态方式呈现“C”,而不会看到“B”,当它被解除时,“B”应该已经存在了。
我一直在看这个帖子:
Present multiple modal view controllers? Presenting a view controller with transparency and animation但由于我需要推送“B”(不是模态的)我不能使用UIModalPresentationCurrentContext
我也尝试过这样做:How to push two view controllers but animate transition only for the second one?
这几乎是我想要的,但我希望“C”以模态样式呈现,例如垂直封面。
【问题讨论】:
【参考方案1】:我终于把这两个混合在一起了:
How to push two view controllers but animate transition only for the second one?
NSMutableArray *controllers = [self.navigationController.viewControllers mutableCopy];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
MyCViewController *vcC = [storyboard instantiateViewControllerWithIdentifier:IDENTIFIER_VC];
MyBViewController *vcB = [storyboard instantiateViewControllerWithIdentifier:IDENTIFIER_VC];
[controllers addObject:vcC];
[controllers addObject:vcB];
self.navigationController.delegate = vcB;
[self.navigationController setViewControllers:controllers animated:YES];
https://www.objc.io/issues/5-ios7/view-controller-transitions/
在 vcB 中:
-(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
if (operation == UINavigationControllerOperationPush || operation == UINavigationControllerOperationPop)
MyAnimator *animator = [[MyAnimator alloc] init];
animator.presenting = (operation == UINavigationControllerOperationPush);
return animator;
return nil;
在 MyAnimator.m 中:
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext
return 0.4;
-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
[[transitionContext containerView] addSubview:toViewController.view];
CGRect currentFrame = toViewController.view.frame;
CGRect toFrameInitial = self.presenting ? CGRectOffset(currentFrame, 0, CGRectGetHeight(currentFrame)) : CGRectOffset(currentFrame, 0, -CGRectGetHeight(currentFrame));
CGRect fromFrameFinal = self.presenting ? CGRectOffset(currentFrame, 0, -CGRectGetHeight(currentFrame)) : CGRectOffset(currentFrame, 0, CGRectGetHeight(currentFrame));
toViewController.view.frame = toFrameInitial;
[UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^
toViewController.view.frame = currentFrame;
fromViewController.view.frame = fromFrameFinal;
completion:^(BOOL finished)
[transitionContext completeTransition:![transitionContext transitionWasCancelled]];
];
【讨论】:
以上是关于呈现 2 个视图控制器,但只有一个过渡的主要内容,如果未能解决你的问题,请参考以下文章