UINavigationController with Push and Pop custom Animation

I'm doing some experiments with animated transition for Pop and Push in UINavigationController and I have some problems with Pop animation.

In brief, it seems that in the Pop animation when the toView is created the system remember the final positin of the view in the Push Animation but not the rotation and, in my case the view is created with swapped width and height.


This is the code for the Push Animation:

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
  
        let container = transitionContext.containerView()!
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
  
        print("Push Start Frame: \(fromView.frame)")
        let π : CGFloat = 3.14159265359
  
        let offScreenRight = CGAffineTransformMakeRotation(-π/2)
        let offScreenLeft = CGAffineTransformMakeRotation(π/2)
  
        toView.transform = self.presenting ? offScreenRight : offScreenLeft
  
        toView.layer.anchorPoint = CGPoint(x:0, y:0)
        fromView.layer.anchorPoint = CGPoint(x:0, y:0)
  
        toView.layer.position = CGPoint(x:0, y:0)
        fromView.layer.position = CGPoint(x:0, y:0)
  
        container.addSubview(toView)
        container.addSubview(fromView)
  
        let duration = self.transitionDuration(transitionContext)
  
        UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: UIViewAnimationOptions.CurveEaseInOut , animations: {
      
            fromView.transform = self.presenting ? offScreenLeft : offScreenRight
            toView.transform = CGAffineTransformIdentity
      
            print("Push End Frame: \(fromView.frame)")
            }, completion: { finished in
                transitionContext.completeTransition(true)
        })
    }

It correctly rotate the vie around the top left corner and prints say:

Push Start Frame: (0.0, 0.0, 320.0, 568.0)

Push End Frame: (-568.0, 0.0, 568.0, 320.0)

And it is ok...


This is the code for my Pop Animation

    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let container   = transitionContext.containerView()!
        let fromView    = transitionContext.viewForKey(UITransitionContextFromViewKey)!
        let toView      = transitionContext.viewForKey(UITransitionContextToViewKey)!
        print("Pop Start Frame: \(toView.frame)")
        let π : CGFloat = 3.14159265359
    
        let offScreenRight = CGAffineTransformMakeRotation(-π/2)
        let offScreenLeft = CGAffineTransformMakeRotation(π/2)
    
        toView.transform = offScreenLeft
        toView.layer.anchorPoint = CGPoint(x:0, y:0)
        fromView.layer.anchorPoint = CGPoint(x:0, y:0)
    
        toView.layer.position = CGPoint(x:0, y:0)
        fromView.layer.position = CGPoint(x:0, y:0)
        container.addSubview(toView)
        container.addSubview(fromView)
    
        let duration = self.transitionDuration(transitionContext)
    
        UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.8, options: UIViewAnimationOptions.CurveEaseOut , animations: {
        
            fromView.transform = offScreenRight
            toView.transform = CGAffineTransformIdentity
            print("Pop End Frame: \(toView.frame)")
            }, completion: { finished in
                transitionContext.completeTransition(true)
        })
    }


And... prints say:

Pop Start Frame: (-320.0, 0.0, 320.0, 568.0)

Pop End Frame: (0.0, 0.0, 568.0, 320.0)


As I sad... it seems that the starting frame remember the position but NOT the rotation.

Does someone can help me to understand what is wrong?


Thank you

UINavigationController with Push and Pop custom Animation
 
 
Q