我今天才用Swift4升级到XCode9。
而且我发现UIViewControllerAnimatedTransitioning不再按预期工作。
此动画的效果是fromView将缩小为0.95,并且toView将从右侧滑入。 pop操作将相反地进行。
但是现在当我单击NavigationBar的后退按钮时,toView的开始位置不正确。它显示了原始大小的toView,然后放大到1.05。

这是我实现过渡动画器的方法。

// animate a change from one viewcontroller to another
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    // get reference to our fromView, toView and the container view that we should perform the transition in
    let container = transitionContext.containerView
    let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
    let toView = transitionContext.view(forKey: UITransitionContextViewKey.to)!

    // set up from 2D transforms that we'll use in the animation
    let offScreenRight = CGAffineTransform(translationX: container.frame.width, y: 0)
    let offScreenDepth = CGAffineTransform(scaleX: 0.95, y: 0.95)

    // start the toView to the right of the screen
    if( presenting ){
        toView.transform = offScreenRight

        container.addSubview(fromView)
        container.addSubview(toView)
    }
    else{
        toView.transform = offScreenDepth

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

    // get the duration of the animation
    // DON'T just type '0.5s' -- the reason why won't make sense until the next post
    // but for now it's important to just follow this approach
    let duration = self.transitionDuration(using: transitionContext)

    // perform the animation!
    // for this example, just slid both fromView and toView to the left at the same time
    // meaning fromView is pushed off the screen and toView slides into view
    // we also use the block animation usingSpringWithDamping for a little bounce

    UIView.animate(withDuration: duration, delay: 0.0, options: .curveEaseOut, animations: {

        if( self.presenting ){
            fromView.transform = offScreenDepth
        }
        else{
            fromView.transform = offScreenRight
        }

        toView.transform = .identity

    }, completion: { finished in

        transitionContext.completeTransition(!transitionContext.transitionWasCancelled)

    })
}

在此迁移指南页面中没有发现任何特别之处。
https://swift.org/migration-guide-swift4/
我应该怎么做才能使转换再次起作用?

最佳答案

在设置其他任何内容之前,请尝试将fromView的转换重置为identity:

// animate a change from one viewcontroller to another
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    ...

    UIView.animate(withDuration: duration, delay: 0.0, options: .curveEaseOut, animations: {

        if( self.presenting ){
            fromView.transform = offScreenDepth
        }
        else{
            fromView.transform = offScreenRight
        }

        toView.transform = .identity

    }, completion: { finished in

        fromView.transform = .identity
        transitionContext.completeTransition(!transitionContext.transitionWasCancelled)

    })

    ....
}

09-11 17:33