自定义水平弹出过渡无法正常工作

Custom horizontal pop transition not working as it should

我正在尝试创建自定义水平滑动 UINavigationController 过渡。推送动画似乎工作正常。但是,当我试图弹出(并向后水平滑动)时,只有一个空白屏幕,并且在动画时间之后,视图出现在它应该出现的位置

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

        let container = transitionContext.containerView
        let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
        let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)!

        let offScreenRight = CGAffineTransform(translationX: container.frame.width, y: 0)
        let offScreenLeft = CGAffineTransform(translationX: -container.frame.width, y: 0)

        fromView.frame = transitionContext.initialFrame(for: transitionContext.viewController(forKey: .from)!)
        toView.frame = transitionContext.finalFrame(for: transitionContext.viewController(forKey: .to)!)

        toView.transform = isPresenting == true ? offScreenRight : offScreenLeft



        container.addSubview(toView)
        container.addSubview(fromView)

        let duration = self.transitionDuration(using: transitionContext)

        UIView.animate(withDuration: duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: [], animations: {

            fromView.transform = self.isPresenting == true ? offScreenLeft : offScreenRight
            toView.transform = .identity

        }, completion: { finished in

            toView.frame = transitionContext.initialFrame(for: transitionContext.viewController(forKey: .from)!)
            transitionContext.completeTransition(true)
        })
    }

您似乎需要重置视图中的任何现有转换。

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {

    let container = transitionContext.containerView
    let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
    let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)!

    // add these two lines      
    fromView.transform = .identity
    toView.transform = .identity

    // ... the rest of your existing code