• Swift: Changing Push / Pop Animations in View Controllers

    Apple Swift LogoI had a requirement to change the navigation animation moving from one view to another in a recent project – based on the newly loaded view being a kind-of external tool in the context of the app I was developing. There seem to be a very small selection of animations available to view navigation, namely:

    • CurlDown
    • CurlUp
    • FlipFromLeft
    • FlipFromRight

    Once settled on your animation of choice, they need adding to the view controller. I have done so using animation blocks:

    For Push:

    UIView.animateWithDuration(0.75, animations: { () -> Void in
        UIView.setAnimationCurve(UIViewAnimationCurve.EaseInOut)
        self.navigationController!.pushViewController(self.newView, animated: false)
        UIView.setAnimationTransition(UIViewAnimationTransition.FlipFromRight, forView: self.navigationController!.view!, cache: false)
    })

    This code was added into the collectionView:didSelectItemAtIndexPath function.

    For Pop:

    The external ‘tool’ that I was loading had already overridden viewWillDisappear:animated: – so in there, I was able to add my animation code, right before calling super.viewWillDisappear:

    UIView.animateWithDuration(0.75, animations: { () -> Void in
        UIView.setAnimationCurve(UIViewAnimationCurve.EaseInOut)
        UIView.setAnimationTransition(UIViewAnimationTransition.FlipFromLeft, forView: self.navigationController!.view, cache: false)
    })
    Post Tagged with , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Are you human: *