martin
martin

Reputation: 1974

Custom animated transition - view in/view out

I have a custom transition written in Swift where the dismissed view goes out as the presented view comes in from the side.

Now I want this same effect, but I want the presented view to come in from the top and the dismissed view go out at the bottom.

My code looks like this:

func animateTransition(transitionContext: UIViewControllerContextTransitioning){

    let container = transitionContext.containerView()
    let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
    let toView = transitionContext.viewForKey(UITransitionContextToViewKey)

    let offScreenRight = CGAffineTransformMakeTranslation(container.frame.width, 0)
    let offScreenLeft = CGAffineTransformMakeTranslation(-container.frame.width, 0)


    if self.presenting == true{
        toView?.transform = offScreenLeft
    }else{
        toView?.transform = offScreenRight
    }

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

    let duration = self.transitionDuration(transitionContext)

    UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.8, options: nil, animations: {

        if self.presenting == true{
            fromView?.transform = offScreenRight
        }else{
            fromView?.transform = offScreenLeft
        }
        toView?.transform = CGAffineTransformIdentity

        }, completion: { finished in

            // tell our transitionContext object that we've finished animating
            transitionContext.completeTransition(true)

    })
}

I thought this would be easy, as my logic tells the way to do this is to change from "width" to "height" in the toView, and fromView, but this does not work, and just creates the same effect as before, but it seems to skip one empty (black) view.

Any suggestions on how to achieve the desired effect would be appreciated.

The buggy code:

func animateTransition(transitionContext: UIViewControllerContextTransitioning){

    let container = transitionContext.containerView()
    let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
    let toView = transitionContext.viewForKey(UITransitionContextToViewKey)

    let offScreenUp = CGAffineTransformMakeTranslation(container.frame.height, 0)
    let offScreenDown = CGAffineTransformMakeTranslation(-container.frame.height, 0)


    if self.presenting == true{
        toView?.transform = offScreenDown
    }else{
        toView?.transform = offScreenUp
    }

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

    let duration = self.transitionDuration(transitionContext)

    UIView.animateWithDuration(duration, delay: 0.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.8, options: nil, animations: {

        if self.presenting == true{
            fromView?.transform = offScreenUp
        }else{
            fromView?.transform = offScreenDown
        }
        toView?.transform = CGAffineTransformIdentity

        }, completion: { finished in

            // tell our transitionContext object that we've finished animating
            transitionContext.completeTransition(true)

    })
}

Upvotes: 0

Views: 277

Answers (1)

rakeshbs
rakeshbs

Reputation: 24572

You are still trying to translate in the X Coordinate. Try doing the translation in the Y coordinate.

let offScreenUp = CGAffineTransformMakeTranslation(0,container.frame.height)
let offScreenDown = CGAffineTransformMakeTranslation(0,-container.frame.height)

Upvotes: 3

Related Questions