How to use UIViewControllerAnimatedTransitioning with UINavigationController?

How can I get custom transitions (iOS7) when I click the view controller on the UINavigationController ? I tried to set TransitioningDelegate both in the UINavigationController and on the controller that I click. Methods are never called.

All the examples I find use custom transitions when presented modally.

+6
source share
4 answers

@rounak has the right idea, but sometimes it helps to prepare code without having to download from github.

Here are the steps I took:

  • Make your FromViewController.m appropriate to UINavigationControllerDelegate . Another code example indicates that you must match UIViewControllerTransitioningDelegate , but this is only if you represent ToViewController.

    @interface ViewController: UIViewController

  • Return your custom transition animation object to the delegate callback method in FromViewController:

     - (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { TransitionAnimator *animator = [TransitionAnimator new]; animator.presenting = (operation == UINavigationControllerOperationPush); return animator; } 
  • Create your own animator class and insert these methods:

     - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext { return 0.5f; } - (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext { // Grab the from and to view controllers from the context UIViewController *fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey]; UIViewController *toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey]; // Set our ending frame. We'll modify this later if we have to CGRect endFrame = CGRectMake(80, 280, 160, 100); if (self.presenting) { fromViewController.view.userInteractionEnabled = NO; [transitionContext.containerView addSubview:fromViewController.view]; [transitionContext.containerView addSubview:toViewController.view]; CGRect startFrame = endFrame; startFrame.origin.x += 320; toViewController.view.frame = startFrame; [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ fromViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeDimmed; toViewController.view.frame = endFrame; } completion:^(BOOL finished) { [transitionContext completeTransition:YES]; }]; } else { toViewController.view.userInteractionEnabled = YES; [transitionContext.containerView addSubview:toViewController.view]; [transitionContext.containerView addSubview:fromViewController.view]; endFrame.origin.x += 320; [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{ toViewController.view.tintAdjustmentMode = UIViewTintAdjustmentModeAutomatic; fromViewController.view.frame = endFrame; } completion:^(BOOL finished) { [transitionContext completeTransition:YES]; }]; } } 

In fact, an animator is an object that makes a heavy lift. Of course, you can make your UINavigationControllerDelegate a separate object, but it depends on how your application architect is.

+19
source

objc.io post on controller transitions are specifically designed to push and display controllers. http://objc.io/issue-5/view-controller-transitions.html

I made this animation ( http://i.imgur.com/1qEyMu3.gif ) solely based on the objc.io post.

In short, you should have a class that implements UINavigationControllerDelegate and UIViewControllerAnimatedTransitioning with the necessary methods to return the correct animator and perform animations.

+4
source

You can see my demo project, which demonstrates the use of custom transitions in the UINavigationController . Take a look at https://github.com/Vaberer/BlurTransition .

+2
source

EDIT: Just realized that this might not answer your question. But this is an alternative.

If you are using a storyboard, you can perform a custom transition by creating a custom segment. In the attribute inspector, change the name of the segue class to your own transition class, for example. MySegue . Then create the MySegue class and implement the -(void)perform method to complete your transition.

 - (void) perform{ UIViewController *source = self.sourceViewController; UIViewController *destination = self.destinationViewController; [UIView transitionFromView:source.view toView:destination.view duration:0.50f options:UIViewAnimationOptionTransitionFlipFromTop completion:nil]; } 
+1
source

All Articles