IOS实战之自定义转场动画详解

2020-01-14 20:18:51丽君

FromViewController实现UINavigationControllerDelegate协议的具体操作如下:


func navigationController(navigationController: UINavigationController, 
   animationControllerForOperation operation: UINavigationControllerOperation, 
           fromViewController fromVC: UIViewController, 
             toViewController toVC: UIViewController) 
            -> UIViewControllerAnimatedTransitioning? {
    if operation == .Push {
      return PushAnimator()
    }
    if operation == .Pop {
      return PopAnimator()
    }
    return nil;
  }

至于animator,就和此前没有任何区别了。可见,一个封装得很好的animator,不仅能在present/dismiss时使用,甚至还可以在push/pop时使用。

UINavigationController也可以添加交互式转场动画,原理也和此前类似。

总结
对于非交互式动画,需要设置presentedViewController的transitioningDelegate属性,这个代理需要为present和dismiss提供animator。在animator中规定了动画的持续时间和表现逻辑。

对于交互式动画,需要在此前的基础上,由transitioningDelegate属性提供交互式动画控制器。在控制器中进行事件处理,然后更新动画完成进度。

对于自定义动画,可以通过UIPresentationController中的四个函数自定义动画执行前后的效果,可以修改presentedViewController的大小、外观并同步执行其他的动画。

自定义动画的水还是比较深,本文仅适合做入门学习用,欢迎互相交流。



注:相关教程知识阅读请移步到IOS开发频道。