导航控制器自定义过渡动画


74

我一直在遵循一些教程来创建自定义动画,同时从一个视图过渡到另一个视图。

我的测试项目从此处使用自定义segue可以正常工作,但是有人告诉我,不再鼓励在自定义segue中进行自定义动画,我应该使用UIViewControllerAnimatedTransitioning

我遵循了一些使用该协议的教程,但是所有这些教程都是关于模式表示的(例如本教程)。

我想做的是在导航控制器树中进行推送,但是当我尝试通过显示(推送)进行相同的操作时,它将不再起作用。

请告诉我在导航控制器中执行从一个视图到另一个视图的自定义过渡动画的正确方法。

而且无论如何,我可以对所有过渡动画使用一种方法吗?如果有一天我想做同样的动画,但是最终不得不重复两次代码以进行模态与控制器转换,那将很尴尬。


@Rob Urgh很抱歉,这听起来很愚蠢,但如何使视图控制器成为导航控制器的代表?我似乎无法将它们绑定在情节提要上,而且我的“ navigationController:animationControllerForOperation:fromViewController:toViewController:”从未被调用。
AVAVT 2014年

你可以做self.navigationController.delegate = self;viewDidLoad。除非您还在@interface行中指定视图控制器符合<UINavigationControllerDelegate>协议,否则您可能会收到警告。
罗布2014年

该代码为循环过渡的swift 4。stackoverflow.com/a/49321985/8334818
Pramod更多

Answers:


196

要使用导航控制器(UINavigationController)进行自定义转换,您应该:

  • 定义您的视图控制器以符合UINavigationControllerDelegate协议。例如,您可以在视图控制器的.m文件中具有私有类扩展名,以指定对此协议的符合性:

    @interface ViewController () <UINavigationControllerDelegate>
    
    @end
    
  • 确保您确实将视图控制器指定为导航控制器的委托:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        self.navigationController.delegate = self;
    }
    
  • 实现animationControllerForOperation您的视图控制器:

    - (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                      animationControllerForOperation:(UINavigationControllerOperation)operation
                                                   fromViewController:(UIViewController*)fromVC
                                                     toViewController:(UIViewController*)toVC
    {
        if (operation == UINavigationControllerOperationPush)
            return [[PushAnimator alloc] init];
    
        if (operation == UINavigationControllerOperationPop)
            return [[PopAnimator alloc] init];
    
        return nil;
    }
    
  • 实现用于推送和弹出动画的动画制作器,例如:

    @interface PushAnimator : NSObject <UIViewControllerAnimatedTransitioning>
    
    @end
    
    @interface PopAnimator : NSObject <UIViewControllerAnimatedTransitioning>
    
    @end
    
    @implementation PushAnimator
    
    - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
    {
        return 0.5;
    }
    
    - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
    {
        UIViewController* toViewController   = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
        [[transitionContext containerView] addSubview:toViewController.view];
    
        toViewController.view.alpha = 0.0;
    
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            toViewController.view.alpha = 1.0;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    }
    
    @end
    
    @implementation PopAnimator
    
    - (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext
    {
        return 0.5;
    }
    
    - (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext
    {
        UIViewController* toViewController   = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
        UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    
        [[transitionContext containerView] insertSubview:toViewController.view belowSubview:fromViewController.view];
    
        [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
            fromViewController.view.alpha = 0.0;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    }
    
    @end
    

    确实会出现淡入淡出的过渡,但是您应该随心所欲地自定义动画。

  • 如果要处理交互式手势(例如,像从左到右弹出的本机滑动之类的东西),则必须实现一个交互控制器:

    • 为交互控制器(符合的对象UIViewControllerInteractiveTransitioning)定义属性:

      @property (nonatomic, strong) UIPercentDrivenInteractiveTransition *interactionController;
      

      UIPercentDrivenInteractiveTransition是一个很好的对象,它会根据手势的完成程度来繁琐地更新自定义动画。

    • 将手势识别器添加到视图。在这里,我只是实现左手势识别器来模拟弹出:

      UIScreenEdgePanGestureRecognizer *edge = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFromLeftEdge:)];
      edge.edges = UIRectEdgeLeft;
      [view addGestureRecognizer:edge];
      
    • 实现手势识别处理程序:

      /** Handle swipe from left edge
       *
       * This is the "action" selector that is called when a left screen edge gesture recognizer starts.
       *
       * This will instantiate a UIPercentDrivenInteractiveTransition when the gesture starts,
       * update it as the gesture is "changed", and will finish and release it when the gesture
       * ends.
       *
       * @param   gesture       The screen edge pan gesture recognizer.
       */
      
      - (void)handleSwipeFromLeftEdge:(UIScreenEdgePanGestureRecognizer *)gesture {
          CGPoint translate = [gesture translationInView:gesture.view];
          CGFloat percent   = translate.x / gesture.view.bounds.size.width;
      
          if (gesture.state == UIGestureRecognizerStateBegan) {
              self.interactionController = [[UIPercentDrivenInteractiveTransition alloc] init];
              [self popViewControllerAnimated:TRUE];
          } else if (gesture.state == UIGestureRecognizerStateChanged) {
              [self.interactionController updateInteractiveTransition:percent];
          } else if (gesture.state == UIGestureRecognizerStateEnded) {
              CGPoint velocity = [gesture velocityInView:gesture.view];
              if (percent > 0.5 || velocity.x > 0) {
                  [self.interactionController finishInteractiveTransition];
              } else {
                  [self.interactionController cancelInteractiveTransition];
              }
              self.interactionController = nil;
          }
      }
      
    • 在导航控制器委托中,还必须实现interactionControllerForAnimationController委托方法

      - (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
                               interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController {
          return self.interactionController;
      }
      

如果您在Google中搜索“ UINavigationController自定义过渡教程”,那么您将获得很多成功。或参阅WWDC 2013 Custom Transitions视频


以及如何保留默认的向后滑动(屏幕边缘手势)?
kidsid49 2015年

@ kidsid49-执行自定义过渡时,您会丢失内置的交互式弹出手势,但也可以选择实现自己的交互控制器。观看该WWDC视频以获取更多信息。请参阅有关的讨论UIPercentDrivenInteractiveTransition(这大大简化了过程)。我在上面添加了一些相关的代码片段。
罗布

@Rob感谢您提出如此详细的答案,我能够将自定义动画成功添加到我的应用程序中。不过,我确实有一个查询,我添加了一个自定义弹出式动画,以便当用户从左向右滑动时,就可以进入左侧的视图,就像没有自定义动画的普通弹出式窗口一样。问题是我似乎无法从左侧进入视图以完全跟随我的拇指/手指,在我触摸的位置与左侧视图边界之间始终存在一定距离,我该如何做?它的视界就是我触摸的地方?
Shumais Ul Haq

您如何触发此推送/弹出过渡。我尝试在情节提要中创建segues(例如显示Push),并在NavigationController中使用viewControllers中的方法执行performSegueWithIdentifier,并使用按描述的方式处理过渡的委托,但它不起作用。那么如何使这种过渡开始呢?
Marcin Kapusta,

1
@Pavan-您animationControllerForOperation可以只检查fromVCand / or toVCnil如果不想让它执行自定义动画,则返回。最简单的就是检查它是否是一个特定的类。更优雅地讲,您可以设计一个视图控制器可以遵循的可选协议,并带有一些boolean属性来指示它们是否需要自定义的push / pop。
罗布

15

您可能想在之前添加以下代码 addSubview

  toViewController.view.frame = [transitionContext finalFrameForViewController:toViewController];

来自另一个问题,在iOS 9上使用navigationcontroller为推送动画进行自定义转换

从Apple的finalFrameForViewController文档中:

返回指定视图控制器视图的结束帧矩形。

此方法返回的矩形表示转换结束时相应视图的大小。对于演示过程中涉及的视图,此方法返回的值可能是CGRectZero,但也可能是有效的框架矩形。


4
哇,这解决了我的问题。为什么这些信息似乎无处不在?像Apple文档一样,其他教程也是如此。
大卫,

1
关键是我正在使用自动版式并在其他地方设置框架...
David

9

使用Rob&Q i的完美答案,这是简化的Swift代码,对.push和.pop使用相同的淡入淡出动画:

extension YourViewController: UINavigationControllerDelegate {
    func navigationController(_ navigationController: UINavigationController,
                              animationControllerFor operation: UINavigationControllerOperation,
                              from fromVC: UIViewController,
                              to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        //INFO: use UINavigationControllerOperation.push or UINavigationControllerOperation.pop to detect the 'direction' of the navigation

        class FadeAnimation: NSObject, UIViewControllerAnimatedTransitioning {
            func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
                return 0.5
            }

            func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
                let toViewController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)
                if let vc = toViewController {
                    transitionContext.finalFrame(for: vc)
                    transitionContext.containerView.addSubview(vc.view)
                    vc.view.alpha = 0.0
                    UIView.animate(withDuration: self.transitionDuration(using: transitionContext),
                    animations: {
                        vc.view.alpha = 1.0
                    },
                    completion: { finished in
                        transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
                    })
                } else {
                    NSLog("Oops! Something went wrong! 'ToView' controller is nill")
                }
            }
        }

        return FadeAnimation()
    }
}

不要忘记在YourViewController的viewDidLoad()方法中设置委托:

override func viewDidLoad() {
    //...
    self.navigationController?.delegate = self
    //...
}

5

它既可以快速运行3也可以快速运行4

@IBAction func NextView(_ sender: UIButton) {
  let newVC = self.storyboard?.instantiateViewControllerWithIdentifier(withIdentifier: "NewVC") as! NewViewController

  let transition = CATransition()
  transition.duration = 0.5
  transition.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
  transition.type = kCATransitionPush
  transition.subtype = kCAGravityLeft
  //instead "kCAGravityLeft" try with different transition subtypes

  self.navigationController?.view.layer.add(transition, forKey: kCATransition)
  self.navigationController?.pushViewController(newVC, animated: false)
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.