4

I have implemented a few interactive UIViewController presentation animations with a UIPanGestureRecognizer, like panning up to present a view controller.

What I'm trying to do now is allow this behavior to continue through one of the view controller animations. So if I slowly pan upwards, which presents a view controller, I want the gesture recognizer that belongs to the newly presented view controller to seamlessly pick up the UITouches that belonged to the previously presented view.

The idea is that if you start on VC1, and then pan up slowly, it would present a view controller. If you continue panning up, it would present another view controller.

Preferably this would done by a UIPanGestureRecognizer that belongs to each ViewController in the stack.

Has anyone done something like this? I am currently trying to cancel the previous gesture recognizer after it presents a VC, but I am not seeing the next VC's gesture recognizer becoming active...

Thanks in advance for any insight someone might be able to provide!

1 Answers1

-1

A gesture recognizer belongs to a view not to a viewController. The top level view in your application is the UIWindow instance. You can add the pan gesture recognizer to the window.

In the storyboard I've set a UINavigationController as the initial viewcontroller.

Storyboard

The only code I've written to test my idea was this

#import "AppDelegate.h"

@interface AppDelegate () {
    CGFloat pos;
    int cnt;

}

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UIPanGestureRecognizer* panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
    [self.window addGestureRecognizer:panRecognizer];

    return YES;
}

-(void) handlePan:(UIPanGestureRecognizer*)recognizer {

    switch (recognizer.state) {
        case UIGestureRecognizerStateBegan: {
            pos = [recognizer locationInView:self.window].y;
            break;
        }
        case UIGestureRecognizerStateChanged: {
            CGFloat newPos = [recognizer locationInView:self.window].y;
            if (pos - newPos > 50) {
                UIViewController* nextVC = [[UIViewController alloc] init];

                nextVC.view.backgroundColor = (cnt++)%2 ? [UIColor redColor] : [UIColor blueColor];
                UINavigationController* navCtrl = (UINavigationController*)self.window.rootViewController;
                [navCtrl pushViewController:nextVC animated:YES];
                pos = newPos;
            }
            break;
        }
        default:
            break;
    }
}

@end
fabe
  • 718
  • 5
  • 10