gpt4 book ai didi

ios - 如何在 UINavigationController 中使用 UIViewControllerAnimatedTransitioning?

转载 作者:技术小花猫 更新时间:2023-10-29 10:56:45 25 4
gpt4 key购买 nike

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

我发现的所有示例在模态呈现时都使用自定义过渡。

最佳答案

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

以下是我采取的步骤:

  1. 使您的 FromViewController.m 符合 UINavigationControllerDelegate。其他示例代码告诉您遵守 UIViewControllerTransitioningDelegate,但这只是在您呈现 ToViewController 的情况下。

    @interface ViewController : UIViewController

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

    - (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. 创建您的自定义动画师类并粘贴这些示例方法:

    - (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 成为一个单独的对象,但这取决于您的应用架构师的方式。

关于ios - 如何在 UINavigationController 中使用 UIViewControllerAnimatedTransitioning?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22884111/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com