简体   繁体   中英

How can I animate the transition between two modal view controllers?

Edited original post for clarity

I'd like to be able to control the animation between two modal view controllers. Specifically, I'd like to have the first modal slide down and the second modal fade in behind it. I just watched the WWDC videos on custom animation transitions and I think that is probably what I want. My thought is that during the sliding down of modal1, I can fade in modal2 (or an image of modal2) and then just present modal2 normally once the animation is finished. I'm not sure if this is the right way to do it so I'm open to ideas.

This is how I'm dismissing/presenting:

 _modal1.modalPresentationStyle = UIModalPresentationCustom;
 [_modal1 setTransitioningDelegate:self];
 [_modal1 dismissViewControllerAnimated:YES completion:^{
        [self presentViewController:_modal2 animated:NO completion:nil];
  }];

And here's my delegate method:

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return [CustomDismissAnimation new];
}

And my attempt at a transition:

@implementation CustomDismissAnimation

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext {

    UIView *containerView = [transitionContext containerView];
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];

    CGRect containerFrame = containerView.frame;
    CGRect fromViewFinalFrame = CGRectMake(0,
                                           containerFrame.size.height,
                                           fromView.frame.size.width,
                                           fromView.frame.size.height);

    UIViewController *test = [UIViewController new];
    [test setBackgroundColor:[UIColor greenColor]];
    test = containerFrame;
    [containerView addSubview:test];
    [containerView bringSubviewToFront: test];

    [UIView animateWithDuration:[self transitionDuration:transitionContext]
                     animations:^{
                         [fromView setFrame:fromViewFinalFrame];
                     }
                     completion:^(BOOL finished){
                         [test removeFromSuperview];
                         [transitionContext completeTransition:YES];
                     }];
}

@end

My expectation would be that this green test view would appear behind modal1 as it slid down the screen but that doesn't seem to be the case. It doesn't show up at all. Any ideas?

Thanks!

一个简单的解决方法是您不能显示2个模态VC,即仅创建一个具有2个视图的模态,前面是旧的,后面是新的(如果要分离VC,则使newVC作为出现的VC的子代),并管理它们之间的隐藏/显示,也可以直接对视图进行动画处理

So you are saying A presents B, then dismisses B, then presents C, and yet A should not visible while dismissing B and presenting C.

One obvious solution would be to let B present C! When you take C down you could unwind all the way thru B to A in one move.

If you insist upon this hierarchy — that is, if A is present B and then C — B and C cannot occupy the space at the same time, because one view controller (A) cannot present two VCs at once. So something is going to appear behind B as it vanishes and then behind C as it appears. However, you are free to say what that is; you could, for example, interpose a "dimming view" in front of A.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM