Search code examples
iosobjective-cuigesturerecognizerglkview

GLKView gesture recognizer without a GLKViewController


I have an application that uses a GLKView, with no other UI elements. I'm not using a GLKViewController, and I'm not planning to.

All of my application setup occurs in didFinishLaunchingWithOptions

My AppDelegate interface is defined as:

@interface AppDelegate : UIResponder <UIApplicationDelegate, GLKViewDelegate, UIGestureRecognizerDelegate>

As part of this setup, I'm trying to set up GestureRecognizer's (Tap, Pinch, etc) - however they appear to not be firing.

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
tapGesture.delegate = self;
tapGesture.numberOfTapsRequired = 1;
tapGesture.numberOfTouchesRequired = 1;
tapGesture.cancelsTouchesInView = NO;
[view addGestureRecognizer:tapGesture];
tapGesture.delegate = self;

My selector is defined as:

#pragma mark - GLKViewDelegate

- (void) handleTapFrom: (UITapGestureRecognizer *)recognizer
{
    // Code to respond to gesture here
    NSLog (@"tapGestureUpdated()");
}

From all the example code I can find, gesture recognisers are usually setup within the viewDidLoad method. However it seems that the GLKView does not provide this method - when I attempt to provide one, it isn't called.

Presumably, this is something that the GLKViewController provides - but as mentioned, I'm not using a GLKViewController.

Note that I am able to override the touchesBegan method (and friends), and these selectors get fired as expected.

Any idea what I'm doing wrong, or if there is a workaround?


Solution

  • So, I managed to get this working.

    At the beginning of the didFinishLaunchingWithOptions I create a very simple UIViewController, along the lines of:

    static UIViewController *viewController;
    viewController = [[UIViewController alloc] init];
    
    [self.window setRootViewController:viewController];
    

    I then add the gesture to the view controller, rather than the view.

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
    tapGesture.delegate = self;
    tapGesture.numberOfTapsRequired = 1;
    tapGesture.numberOfTouchesRequired = 1;
    tapGesture.cancelsTouchesInView = NO;
    //[view addGestureRecognizer:tapGesture];
    [viewController.view addGestureRecognizer:tapGesture];
    

    This seems suspect, since I've not defined what the view controller's view should be. But it seems the view controller will happily receive these events and fire on these events.