最近项目中有一个视频回放页,一期的时候直接present了一个controller,然后加载播放器进行视频的回放。二期的时候加了新的需求,可以从回放页面继续下一级跳转,但是由于回放页面是present进来的,导致后面的push和present出现诸多问题。所以自己就尝试着,重写NavgationController的转场动画,用push模拟出present的动画效果。
这里用到了UIViewControllerAnimatedTransitioning和UINavigationControllerDelegate两个系统协议,在需要VC切换的时候系统会向实现了UIViewControllerAnimatedTransitioning协议的对象询问是否需要使用自定义的切换效果,重头戏就在这里:
有push就有对应的pop,所以我们首先实现两个PushAnimator。
import UIKit
class PushAnimation: NSObject, UIViewControllerAnimatedTransitioning {
//设置push动画的周期为0.25s
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.25
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
//就是push操作后即将显示的controller
let toController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as! PopController
//containerView是一个装载view的容器,转场动画中我们所看见的视图,其实都是add在containerView上
let containerView = transitionContext.containerView
//toView的最终大小,默认是全屏大小
let toViewFinalFrame = transitionContext.finalFrame(for: toController)
//获取toView,同样还可以用UITransitionContextViewKey.from获取fromView
let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)
//动画还未开始的时候,让toView在屏幕下方,这样开启动画后,才会有从下往上的一个动画过程
toView?.frame = CGRect(x: 0, y: UIScreen.main.bounds.height, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
//将toView添加到containerView上
if toView != nil {
containerView.addSubview(toView!)
}
//开始启动动画
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: .curveEaseInOut, animations: {
toView?.frame = toViewFinalFrame
}) { _ in
//动画完成后调用,会从containerView删除fromView
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
}
接下来就是popAnimator,代码稍稍有差别
import UIKit
import UIKit
class PopAnimation: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.25
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let toController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as! PushController
let containerView = transitionContext.containerView
let toViewFinalFrame = transitionContext.finalFrame(for: toController)
let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)
let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)
if toView != nil {
//因为需要fromView从上到下然后消失,最后就显示了toView,所以需要将toView放在containerView的最下面
containerView.addSubview(toView!)
containerView.sendSubview(toBack: toView!)
}
UIView.animate(withDuration: transitionDuration(using: transitionContext), delay: 0, options: .curveEaseInOut, animations: {
toView?.frame = toViewFinalFrame
//fromView从上到下进行一个位移动画,不需要removeFromSuperView
fromView?.frame = CGRect(x: 0, y: UIScreen.main.bounds.height, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
}) { _ in
//执行这句话系统会从containerView上移除fromView
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
}
第二步是实现UINavigationControllerDelegate协议,分别在push和pop的时候选择合适的Animator进行转场
import UIKit
class NavigationAnimationDelegate: NSObject, UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
if fromVC is PushController && operation == .push {
return PushAnimation()
} else if operation == .pop && fromVC is PopController {
return PopAnimation()
} else {
return nil
}
}
}