Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIPageViewController vs UIViewControllerInteractiveTransitioning

Tags:

Simple question, I've done a decent amount of exploration around custom navigation / transitions between UIViewControllers and am unclear about the following:

I'm looking for behavior similar to what UIPageViewController provides (non stack-based navigation forward and backwards through "pages" of content). But I want to be able to customize the transitions, and I want the transitions to be interactive linked to a custom UIPanGestureRecognizer.

It seems like the UIViewControllerInteractiveTransitioning protocol provides some of what I want (interactivity, custom transitions). But because transitions are invoked solely with presentViewController:animated: and dismissViewControllerAnimated: it seems like it's built exclusively for use with stack-based navigation (ie UINavigationController, UITabBarController, modal presentation). Ie it doesn't seem like it'll play nice with something like UIPageViewController.

If I use UIViewController containment to build a custom container similar to UIPageViewController (see in progress demo here) can I integrate the UIViewControllerInteractiveTransitioning protocol into this to drive the transitions? Or do I need to roll those on my own (currently I have a rough manual implementation of interactive transitions)?

like image 629
Alfie Hanssen Avatar asked Sep 25 '13 16:09

Alfie Hanssen


1 Answers

I do a lot of custom animations in my apps that most developers shy away from because I use a lot of UIViewControlloer Containment in my work.

It's the simplest way to get the transitions that you're looking for.

Here's how I would go about it:

  1. Create a base view controller; lets call it MainViewController. It will have references to all of the other view controllers and hold the logic for the transitions. It should also follow a protocol we'll define as ViewXControllerDelegate.

  2. Create your other view controllers; lets call them View1Controller, View2Controller, View3Controller. Add an instance of each of them as private properties of MainViewController. In the init method of MainViewController, instantiate them and add their views as subviews of MainViewController's view. Should look something like this:

    self.v1c = [[View1Controller alloc]init];
    [self addChildViewController:self.v1c];
    [self.v1c didMoveToParentViewController:self];
    
    //Setup each subview so that its frame makes it off screen or
    //On screen depending on the app state and where you want each
    //subview to animate to/from
    
    [self.view addSubview:self.v1c.view];
    
    ....
    
  3. Setup up a UIPanGestureRecognizer in each of your ViewXControllers that has its target and selector set to the parent view controller (MainViewController).

  4. Handle all logic in your MainViewController class where you take the distance swiped, application state, the locations of each of the views into account (using helper properties in the ViewXControllers like "inactiveFrame" or "activeFrame" where the animation between them happens based on the percentage of movement that's occurred in the pan gesture.

like image 63
AlexKoren Avatar answered May 17 '23 23:05

AlexKoren