2

I want to push from a View Controller "A", a second ViewController "B" (with navigation bar) which should show a third modal ViewController "C", which shows a countdown and dismisses itself, to finally show "B".

I would like "C" to be presented modally, without "B" being seen, and when it gets dismissed, "B" should already be there.

I've been looking at this posts:

But since I need "B" to be pushed (not modal) I can't use UIModalPresentationCurrentContext

I have also tried to do this: How to push two view controllers but animate transition only for the second one?

And it's almost what I want, but I'd like "C" to be presented in a modal style, such as cover vertical.

Community
  • 1
  • 1
jdev
  • 569
  • 5
  • 25

1 Answers1

3

I finally ended up doing a mix with these two:

In 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;
    }

In 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]];
    }];
}
Community
  • 1
  • 1
jdev
  • 569
  • 5
  • 25