Chapter 23
View Controller Transitions and Animations

Wouldn't it be great if you could define the transition style between view controllers? Apple provides a handful of default animations for view controller transitions. Presenting a view controller modally usually uses a slide-up animation. The transition between two view controllers in navigation controller is predefined too. Pushing or popping a controller from the navigation controller's stack uses a standard slide animation. With the iOS SDK, developers are allowed to implement our own transitions through the View Controller Transitioning API. The API gives you full control over how one view controller presents another.

There are two types of view controller transitions: interactive and non-interactive. In iOS, you can pan from the leftmost edge of the screen and drag the current view to the right to pop a view controller from the navigation controller's stack. This is a great example of interactive transition. In this chapter, we are going to focus on the non-interactive transition first, as it is easier to understand.

The concept of custom transition is pretty simple. You create an animator object (or so-called transition manager), which implements the required custom transition. This animator object is called by the UIKit framework when one view controller starts to present or transit to another. It then performs the animations and informs the framework when the transition completes.

When implementing non-interactive view controller transitions, you basically deal with the following protocols:

  • UIViewControllerAnimatedTransitioning - your animator object must adopt this protocol to create the animations for transitioning a view controller on or off screen.
  • UIViewControllerTransitioningDelegate - you adopt this protocol to vend the animator objects used to present and dismiss a view controller. Interestingly, you can provide different animator objects to manage the transition between two view controllers.
  • UIViewControllerContextTransitioning - this protocol provides methods for accessing contextual information for transition animations between view controllers. You do not need to adopt this protocol in your own class. Your animator object will receive the context object, provided by the system, during the transition.

It looks a bit complicated, right? Actually, it's not. Once you go through a simple project, you will have a better idea about how to build custom transitions between view controllers.

Demo Project

We are going to build a simple demo app. To keep your focus on building the animations, download the project template from http://www.appcoda.com/resources/swift55/NavTransitionStarter.zip. The template comes with prebuilt storyboard and view controller classes. The user interface was built using collection view. If you do not have any experience with UICollectionView, read over chapter 18 and 20. If you have a trial run, you will end up with a screen similar to the one shown below.

Figure 23.1. The demo app
Figure 23.1. The demo app

Each icon indicates a unique custom transition. For now, the icons are not functional. Coming up next, you will learn how to implement all the transitions using the View Controller Transitioning API.

To continue reading and access the full version of the book, please get the full copy here. You will also be able to access the full source code of the project.

results matching ""

    No results matching ""