iOS默认的push动画是把即将展示的控制器从右边推过来。有时我们想实现类似PPT中的一些动画,这时候就需要自定义转场动画了。如下图我们想实现一个淡出并且放大的过场动画,在退出时是一个淡出缩小的动画。
首先需要自定义一个类DiaryAnimator.swift遵守 UIViewControllerAnimatedTransitioning 协议,然后实现它,代码如下:
import UIKit class DiaryAnimator: NSObject, UIViewControllerAnimatedTransitioning { // 用于接受外界的operation var operation:UINavigationControllerOperation! // 返回动画时间 func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { return 0.4 } // 要设置的动画 //UIKit calls this method when presenting or dismissing a view controller. Use this method to configure the animations associated with your custom transition. You can use view-based animations or Core Animation to configure your animations. func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { let containerview = transitionContext.containerView let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) let fromView = fromVC!.view let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) let toView = toVC?.view // 设置新场景透明度 toView?.alpha = 0.0 // 设置初始值 if operation == UINavigationControllerOperation.pop { fromView?.transform = CGAffineTransform(scaleX: 1.0, y: 1.0) }else{ toView?.transform = CGAffineTransform(scaleX: 0.3, y: 0.3) } containerview.insertSubview(toView!, aboveSubview: fromView!) UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: UIViewAnimationOptions.curveEaseInOut, animations: { if self.operation == UINavigationControllerOperation.pop { // 放大要转出的场景 fromView?.transform = CGAffineTransform(scaleX: 3.3,y: 3.3) } else { // 设置新场景为原始大小 toView?.transform = CGAffineTransform(scaleX: 1.0,y: 1.0) } toView?.alpha = 1.0 },completion: { finished in transitionContext.completeTransition(true) }) } }
自定义好动画后,在要使用的控制器中实现 UINavigationControllerDelegate 协议的 optional func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? 方法,返回自定义的动画即可
extension HomeYearController : UINavigationControllerDelegate { func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? { let animate = DiaryAnimator() animate.operation = operation return animate } }