当我将“ ThirdViewController”推到“ SecondViewController”时,第二个视图控制器的子视图(viewToGoUp)应该上升,而第三个视图控制器的子视图(comeUpView)应该上升。我尝试了以下方法。使用以下代码,viewToGoUp
class CustomNavAnimator:NSObject,UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
return 0.3
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from) as? SecondViewController else {
//reverseAnimation(using: transitionContext)
return
}
guard let toVC = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to) as? ThirdViewController else {
return
}
transitionContext.containerView.addSubview(toVC.view)
transitionContext.containerView.addSubview(fromVC.view)
fromVC.viewToGoUp.alpha = 1.0
toVC.comeUpView.center = CGPoint(x: toVC.comeUpView.center.x, y: toVC.comeUpView.center.y+500)
UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
fromVC.viewToGoUp.alpha = 0.0
fromVC.viewToGoUp.center = CGPoint(x: fromVC.viewToGoUp.center.x, y: fromVC.viewToGoUp.center.y - 500)
toVC.comeUpView.center = CGPoint(x: toVC.comeUpView.center.x, y: toVC.comeUpView.center.y-500)
}) { (done) in
transitionContext.completeTransition(done)
}
}
}