iosobjective-canimationuiviewcontrollercustom-transition

Custom transition iOS (Push/Pop)


I created a custom zoom-in transition when pushing a new viewController, and it used to work perfectly fine. Now I want to create a zoom-out effect when popping the viewController, and even-thought the final state is correct, the animation is wrong since I don't know how to identify if it is pushing or popping and all the methods like isBeingPresented return false and presentedViewController is always nil

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
{
  self.transitionContext = transitionContext;

  UIView *containerView =  [transitionContext containerView];
  UIViewController *fromViewController = (UIViewController *)[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  UIViewController *toViewController = (UIViewController *) [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

  [containerView addSubview:toViewController.view];

  CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
  scaleAnimation.duration = [self transitionDuration:transitionContext];
  scaleAnimation.delegate = self;
  scaleAnimation.removedOnCompletion = YES;

  CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
  opacityAnimation.duration = [self transitionDuration:transitionContext];
  opacityAnimation.delegate = self;
  opacityAnimation.removedOnCompletion = YES;

  if (toViewController.isBeingPresented) {
    scaleAnimation.fromValue = [NSNumber numberWithDouble:0];
    scaleAnimation.toValue = [NSNumber numberWithDouble:1];

    opacityAnimation.fromValue = [NSNumber numberWithFloat:1];
    opacityAnimation.toValue = [NSNumber numberWithFloat:0];
  } else {
    scaleAnimation.fromValue = [NSNumber numberWithDouble:1];
    scaleAnimation.toValue = [NSNumber numberWithDouble:0];

    opacityAnimation.fromValue = [NSNumber numberWithFloat:0];
    opacityAnimation.toValue = [NSNumber numberWithFloat:1];
  }

  [toViewController.view.layer addAnimation:scaleAnimation forKey:nil];
  [fromViewController.view.layer addAnimation:opacityAnimation forKey:nil];
}

Solution

  • You can save a UINavigationControllerOperation variable:

    -(id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                      animationControllerForOperation:(UINavigationControllerOperation)operation
                                                   fromViewController:(UIViewController*)fromVC
                                                     toViewController:(UIViewController*)toVC {
        self.navigationOperation = operation;
    
        return self;
    }
    

    then you check it is push or pop:

    if (self.navigationOperation == UINavigationControllerOperationPush) {
        // push
    } else if (self.navigationOperation == UINavigationControllerOperationPop) {
        // pop
    }