自定义水平弹出过渡不能正常工作

时间:2017-08-11 14:49:47

标签: ios swift uinavigationcontroller uiviewanimationtransition

我正在尝试创建自定义水平幻灯片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)
        })
    }

1 个答案:

答案 0 :(得分:1)

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

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