2

I just upgraded to XCode9 with Swift4 today. And I found that my UIViewControllerAnimatedTransitioning doesn't work as expected anymore. The effect of this animation is that the fromView will scale down to 0.95 and the toView will slide in from the right side. The pop operation will do it reversely. But now when I hit the back button of the NavigationBar, the start position of the toView isn't right. It displayed a original size toView and then scale up to 1.05.

Here's how I implement the transition animator.

// 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)

    })
}

I didn't found anything special in this migration guide page. https://swift.org/migration-guide-swift4/ What should I do to make the transition works again?

Kimi Chiu
  • 2,103
  • 3
  • 22
  • 37

1 Answers1

1

Before setting anything else, try to reset transformations of the fromView to 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)

    })

    ....
}
Kimi Chiu
  • 2,103
  • 3
  • 22
  • 37
Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
  • Not works. The value of the `transform` seems right. The problems is the displaying. It did transform from 0.95 to 1. But it displayed like transforming from 1 to 1.05. – Kimi Chiu Sep 22 '17 at 09:06
  • well, my guess was that the previous transformation collided somehow with the current.. although that should not be the case.. – Milan Nosáľ Sep 22 '17 at 09:25
  • ill take another look at it a bit later – Milan Nosáľ Sep 22 '17 at 09:35
  • 1
    I found the way to fix it. Just move the `fromView.transform = .identity` to the completion block of `UIView.animate`. Thanks for the hint. – Kimi Chiu Sep 22 '17 at 09:43