如何在UINavigationController中使用UIViewControllerAnimatedTransitioning?

Kru*_*lur 10 cocoa-touch xamarin.ios ios ios7

将视图控制器推到时,如何获得自定义转换(iOS7)UINavigationController?我尝试TransitioningDelegateUINavigationController我正在推动的控制器中设置两者.方法永远不会被调用.

我发现的所有示例在模态呈现时都使用自定义转换.

Jon*_*Jon 21

@rounak有正确的想法,但有时无需从github下载就可以准备好代码.

以下是我采取的步骤:

  1. 使你的FromViewController.m符合UINavigationControllerDelegate.其他示例代码告诉您符合UIViewControllerTransitioningDelegate,但这只是在您展示 ToViewController时.

    @interface ViewController:UIViewController

  2. 在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;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 创建自定义动画类并粘贴这些示例方法:

    - (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];
        }];
    }
    }
    
    Run Code Online (Sandbox Code Playgroud)

从本质上讲,动画师是做重物的对象.当然,您可以将UINavigationControllerDelegate设置为单独的对象,但这取决于您的应用程序的架构师.