【问题标题】:How to use UIViewControllerAnimatedTransitioning with UINavigationController?如何将 UIViewControllerAnimatedTransitioning 与 UINavigationController 一起使用?
【发布时间】:2014-05-18 00:36:14
【问题描述】:

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

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

【问题讨论】:

  • 请在回答您的问题时将答案标记为正确

标签: ios cocoa-touch ios7 xamarin.ios


【解决方案1】:

编辑:刚刚意识到这可能无法回答您的问题。但它是另一种选择。

如果您使用故事板,则可以通过创建自定义转场来进行自定义过渡。 在属性检查器中,将 segue 类名称更改为您的自定义转换类,例如MySegue。然后创建MySegue 类并实现-(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];
}

【讨论】:

    【解决方案2】:

    objc.io 关于视图控制器转换的帖子专门用于推送和弹出视图控制器。 http://objc.io/issue-5/view-controller-transitions.html

    我仅根据 objc.io 帖子制作了这个动画 (http://i.imgur.com/1qEyMu3.gif)。

    简而言之,您必须有一个实现UINavigationControllerDelegateUIViewControllerAnimatedTransitioning 的类以及返回正确动画师和执行动画所需的方法。

    【讨论】:

    • 你能分享动画代码吗,我正在做类似的事情,如图所示。
    • 我在 push 和 pop (0.01,0.01) 期间给 CGAffineTransform(translationX: 1.02, y: 1.02) 似乎工作正常,也想知道你的价值是什么:)跨度>
    【解决方案3】:

    @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;
      }
      
    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 成为一个单独的对象,但这取决于您如何构建您的应用程序。

    【讨论】:

    • 我猜TransitionAnimator 是 UIViewControllerAnimatedTransitioning 类。我猜他在其中添加了属性presenting
    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      • 1970-01-01
      相关资源
      最近更新 更多