如何将 UIViewControllerAnimatedTransitioning 与 UINavigationController 一起使用? [英] How to use UIViewControllerAnimatedTransitioning with UINavigationController?

查看:32
本文介绍了如何将 UIViewControllerAnimatedTransitioning 与 UINavigationController 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在将视图控制器推送到 UINavigationController 时获得自定义转换 (iOS7)?我尝试在 UINavigationController 和我正在推送的控制器上设置 TransitioningDelegate这些方法永远不会被调用.

How can I get custom transitions (iOS7) when pushing a view controller onto UINavigationController? I tried setting the TransitioningDelegate both in the UINavigationController and also on the controller I'm pushing The methods never get called.

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

All examples I find use custom transitions when presenting modally.

推荐答案

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

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

以下是我采取的步骤:

  1. 使您的 FromViewController.m 符合 UINavigationControllerDelegate.其他示例代码告诉您要符合 UIViewControllerTransitioningDelegate,但前提是您呈现 ToViewController.

  1. Make your FromViewController.m conform to UINavigationControllerDelegate. Other sample code out there tells you to conform to UIViewControllerTransitioningDelegate, but that's only if you're presenting the ToViewController.

@interface ViewController : UIViewController

@interface ViewController : UIViewController

在 FromViewController 的委托回调方法中返回您的自定义过渡动画对象:

Return your custom transition animator object in 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 custom animator class and paste these sample 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];
        }];
    }
    }
    

  • 本质上,动画师是承担重任的对象.当然,您可以让 UINavigationControllerDelegate 成为一个单独的对象,但这取决于您的应用程序架构.

    Essentially, the animator is the object doing the heavy lifting. Of course, you can make your UINavigationControllerDelegate be a separate object, but that depends on how your architect your app.

    这篇关于如何将 UIViewControllerAnimatedTransitioning 与 UINavigationController 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆