最近需要自定义一个viewcontroller 的出场和入场动画,发现官方没有提供多种动画供选择,只能自定义,遂记录如下:
1. 实现UINavigationControllerDelegate代理
extension ViewController: UINavigationControllerDelegate {
func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
ifoperation == .push{
returnPushAnimaton()
}elseifoperation == .pop{
returnPopAnimation()
}
returnnil
}
class PushAnimaton: NSObject, UIViewControllerAnimatedTransitioning {
funcanimateTransition(using transitionContext:UIViewControllerContextTransitioning) {
transitionContext.containerView.backgroundColor = UIColor.white // 修改过渡时的背景颜色
lettoViewController = transitionContext.viewController(forKey:UITransitionContextViewControllerKey.to)
letfromViewController = transitionContext.viewController(forKey:UITransitionContextViewControllerKey.from)
transitionContext.containerView.insertSubview((toViewController?.view)!, aboveSubview: (fromViewController?.view)!)
letheight =UIScreen.main.bounds.height
toViewController?.view.transform=CGAffineTransform(translationX:0, y: height)
UIView.animate(withDuration:self.transitionDuration(using: transitionContext), animations: {
toViewController?.view.transform=CGAffineTransform.identity
fromViewController?.view.transform=CGAffineTransform(translationX:0, y: -height)
}) { (completion)in
fromViewController?.view.transform=CGAffineTransform.identity
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
functransitionDuration(using transitionContext:UIViewControllerContextTransitioning?) ->TimeInterval{
return0.3
}
}
class PopAnimation: NSObject, UIViewControllerAnimatedTransitioning {
funcanimateTransition(using transitionContext:UIViewControllerContextTransitioning) {
transitionContext.containerView.backgroundColor = UIColor.white // 修改过渡时的背景颜色
lettoViewController = transitionContext.viewController(forKey:UITransitionContextViewControllerKey.to)
letfromViewController = transitionContext.viewController(forKey:UITransitionContextViewControllerKey.from)
transitionContext.containerView.insertSubview((toViewController?.view)!, aboveSubview: (fromViewController?.view)!)
letheight =UIScreen.main.bounds.height
toViewController?.view.transform=CGAffineTransform(translationX:0, y: -height)
UIView.animate(withDuration:self.transitionDuration(using: transitionContext), animations: {
toViewController?.view.transform=CGAffineTransform.identity
fromViewController?.view.transform=CGAffineTransform(translationX:0, y: height)
}) { (completion)in
fromViewController?.view.transform=CGAffineTransform.identity
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
functransitionDuration(using transitionContext:UIViewControllerContextTransitioning?) ->TimeInterval{
return0.3
}
}
}
2.在viewContorller中设置navigationController的代理
class ViewController: UIViewController {
overridefuncviewDidLoad() {
super.viewDidLoad()
self.navigationController?.delegate = self
}
}
大功告成
网友评论