自定义过渡(视图控制器),如何为toVC的子视图设置动画?

时间:2020-04-22 10:11:50

标签: ios swift animation custom-transition

当我将“ 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)
        }

    }
}

0 个答案:

没有答案