So, it's a pretty cool thing to do a custom navigation transitions between your controllers. I decided to play around with it and the end result turned out to be quite good looking. Why not share with others.
This is the default transition that everyone using iOS is definitely used to.
My idea was to create something a little different. I wanted the transition to go side-by-side where one controller pushes the other out of the way and vice-versa. And to top it off have a global background for all ViewControllers to make them transition from one to another without seeing the controller bounds change.
So to make this scenario we have the following steps to take:
- Make a custom navigation controller, which will overwatch all of the interactions;
- Make a custom animator, which handles the controller frame animations;
- Make a custom interactor, which handles screen panning to allow interactive 'pop' transition;
- Make a custom interactive background;
We start off nice and simple — create a new single view project. Create one UINavigationController subclass and 3 UIViewController subclasses. All of them won't be needed, but they help to make a point. Sometimes.
The Main.storyboard file is set up the following way:
UINavigationController, which is set to subclass to our custom navigation class (BaseNC), it has a RootViewController relationship to VC1. VC1 then has a push segue tied from the 'Push' button to VC2. Same for VC2 to VC3. And VC3, in the end, has a custom action tied to its controller class, where it performs a popToRootViewController call. Nothing special here, just setup.
Setup, done, moving over to BaseNC class to start the setup.
This is the initial setup for our main UINavigationController. Since we're setting our superclass from the storyboard, we are going to be using the decoder initialiser and setting the transition style manually. The commented initialiser will allow us to use this navigation controller for different initialisation types if need be.
The enclosed enum NavigationTransitionStyle will be used to separate transition styles for different needs. In this tutorial, we'll be using only one style, but the idea for reusability remains.
In ViewDidLoad we also add the delegate to self and use an UINavigationController extension to make the navigation bar transparent. That is definitely not necessary but makes the whole look of the transitions much prettier.
Since the code right now doesn't compile due to delegacy, let's make it compliant with the delegate protocol.
This is how the navigation controller looks when adding the compliance to the UINavigationControllerDelegate protocol and adding a custom animator. The animator will ensure that the controllers when pushed and popped move according to our commands.
Step 2: Next to the animator itself.
This is how the SideBySide animator looks in its simple glory. We have an initialiser, which takes the navigation controller and direction as parameters. After that, it's all about setting the incoming and outgoing ViewController frames.
One place that is can maybe be confusing is the different animation calls.
If the animation is performed automatically (back button pressed or a present/pop function called), the user has no control over the transition. But when the left side panning is performed, the TransitionContext is interactive and it looks a bit smoother when the completion animation (panning and releasing) is performed differently from regular push/pop animation. But that's just my opinion, it's all up to interpretation.
In any case, we get the 'from' and 'to' views, insert them and perform the frame animations manually. In this case, the pushed controller is added on the far right side of the screen and then animated in, while the current controller is animated out. The same for popping, but the other way around. That's all straight forward.
Step 3 — Interactor:
Implementing this allows the navigation transitions to perform side-by-side, but there is no interactivity since we're overriding it in our NavigationController. This is the time to add our SideBySideInteractor.
This is the interactor that allows us to perform panning action on the pushed controller and keep the action interactive. Identical to iOS default panning interaction, but we still keep our custom transition active.
For this, to work, we need to have a protocol — NavigationInteractionProxy. This has only one variable — isPerforming. This will communicate with our navigation controller about the actions performed, so we can keep our interaction.
The class initialiser takes 2 arguments — the navigation controller (which is set to weak to avoid leaks) and the animatable controller (to which we add the pan gesture). We also have a completion, but we'll get to that later.
In the gesture handling function, we get the progress of the pan action — this tells us how far we are with the transition.
When we start the transition, we have to pop the ViewController to allow it to start animating. If we release it before the center point of the screen, the transition will be cancelled and the controller returned to it's original position. If after center point, then the pop transition will be completed.
Popping controllers has an interesting feature, that if we start the pan gesture with enough speed (velocity), then we straight up pop the controller to improve user interaction. This is handled in '.ended' case of the gesture state.
Now let's update our navigation controller to add the interactor.
This is how our navigation controller looks with added interactor handling.
The idea is that now we have a private array —
fileprivate var interactors: [NavigationInteractionProxy?] = 
This is required to keep a reference on all the interactors created in the navigation stack. For every pushed controller we have an interactor.
So, why do we need the protocol — that is to tell the transition animation, that we currently are in the process of panning and we should use the interactor. Otherwise, the animation will just complete without user interaction.
One thing that has to be said is that when we're moving to rootViewController, we should get rid of all interactors. This case appears when we have at least 2 controllers on the stack and then we pop to root. The interactors.removeAll() fixes that problem.
This all could have been written much shorter, but the idea was to make it easily adaptable for new interactors and animators. So far I've had no problems introducing new animations in this class.
By adding this we should be able to perform the user interactive panning pop transition.
Step 4: Interactive background
To achieve that we do the following:
- Add a UIScrollView property to the navigation controller class;
- Add a baseOffset property in the BaseNC class — private var backgroundStartOffset: CGFloat = 0
The starting offset will help to reset the image to its original position on rootViewController.
3. Get a large image that can handle at least some scrolling left and right;
4. In BaseNC initialiser call the addUniversalBackground() function;
The image resizing functions will be available in the final project, but they don't do anything special other than making the image scale properly for the screen.
Now we have the background, but we need the interaction, so:
- In storyboard for each ViewController, we set the view background color to clear.
- Add the following code to our BaseNC class:
These are the functions that will move our background together with transitioning controllers.
3. Change the animation block in our TransitionAnimator to:
And that's all there is to it.
If everything is set up correctly and I haven't forgotten anything, the interaction should be somewhat similar to this. But I probably have, so the project link is available at the end to see the end product.
The durations, interactions and handles can all be modified and customised to fit anyone's needs.
Feel free to check out the project here.
Thanks for reading & happy coding.
Receive a new Mobile development related stories first. — Hit that follow button
Twitter: @ ChiliLabs