I'm interested in a concise example of how to create an NSObject
subclass that implements the UIViewControllerInteractiveTransitioning
protocol to manage a custom interactive transition between two UIViewController
s. Ideally in response to a swipe gesture. Something akin to the iOS7 default interactive swipe that now comes with UINavigationController
, but a custom/manual implementation example of this.
I've read the docs:
- UIViewControllerContextTransitioning
- UIPercentDrivenInteractiveTransition
- UIViewControllerInteractiveTransitioning
- UIViewControllerAnimatedTransitioning
And looked at a few examples elsewhere:
- one
- two
- three
- four (I set this up but it's more about
UIViewController
containment
and manual implementation of these transitions rather thanUIViewControllerInteractiveTransitioning
The docs are fairly clear but dont reference sample code. And the examples leave a little to be desired (unanswered questions about how the various pieces are tied together).
So my questions are:
- Can someone help fill in the blanks about how to tie a gesture (e.g. a swipe) to the object that implements the
UIViewControllerInteractiveTransitioning
protocol? - What is the relationship between object implementing the UIViewControllerInteractiveTransitioning protocol and that implementing the UIViewControllerAnimatedTransitioning protocol? Seems like you must have both to trigger interactive transitions...
Thanks in advance...
1) The easiest way to tie a gesture to the
UIViewControllerInteractiveTransitioning
object, is making it subclass ofUIPercentDrivenInteractiveTransition
. Then where you implement the gesture handler, you callupdateInteractiveTransition:
here an example with code:This code is from https://www.captechconsulting.com/blogs/ios-7-tutorial-series-custom-navigation-transitions--more
2) The function
animateTransition
ofUIViewControllerAnimatedTransitioning
is used to perform the interactive transition. It is automatically partitioned in "keyframes" thanks to your previous call toupdateInteractiveTransition
. But I suppose that if you implement yourstartInteractiveTransition:
method ofUIViewControllerInteractiveTransitioning
(so without usingUIPercentDrivenInteractiveTransition
subclass) then you are responsible to manage the fully transition (not sure about that.. sorry but the documentation in my opinion is not really clear).Apple does indeed provide an example project now for how you can achieve this. Having said that, I don't think it's the finest/clearest example, but it should get you on the correct path.
They also have a WWDC video that takes you through this very project.
Be warned that it's a fairly complex example, but if you do manage to break it apart and understand the various pieces, you should be equipped to deal with more or less anything on the transition front.
Essentially, the project splits the problem into two helper classes, 1) an
AssetTransitionController
that is initialised with, and exists for the life of, the view controller, and 2) anAssetTransitionDriver
object that is created at the beginning of, and exists for the duration of, a transition.The
AssetTransitionController
is fairly simple, it conforms toUIViewControllerAnimatedTransitioning
andUIViewControllerInteractiveTransitioning
managing the lifecycle of theAssetTransitionDriver
.The
AssetTransitionDriver
is a simpleNSObject
sub-class but actually ends up far more complex. It manages the mainUIViewPropertyAnimator
, creating the view hierarchy for the transition, and responds to the interaction driver (a pan gesture recogniser). It also vends its animator to theAssetTransitionController
when requested.It doesn't use
UIPercentDrivenInteractiveTransition
at all.