Gesture Recognizers (iPhone OS 3.2) working in OF

Hi folks,

Was just trying out the iPhone OS 3.2 UIGestureRecognizer class today and seems to be working fine…

Thought I’d post here just in case it helps anyone, and also in case anyone sees a nicer way to do it… :slight_smile:

oh nice find – zach and I were just looking at the docs for that, the other day. glad that it’s working / thanks for posting!

take care,
zach

I’m getting an error: “cannot find interface declaration for ‘EAGLView’.” I’m guessing I need to import the framework that declares EAGLView, but I thought that was in UIKit. Am I missing something here? If it matters, I’m building for 4.2.

Thanks,
Zach

Yeah, if I remember correctly, it’s in UIKit.

You need to import the header file with your EAGLView category extension.

Plus maybe modify my code snippit above to make the EAGLView category simultaneously act as a delegate:

@interface EAGLView (Gestures)

Hmm…I’m still getting the same error.

Hi, sorry, I misread: the gestures class is in UIKit, but for EAGLView you need to include the OpenGLES framework.

Are you sure you don’t mean EAGLContext? I don’t see EAGLView defined anywhere.

Your EAGLView should be defined in EAGLView.h which is in addons/ofxiPhone/src. That’s the actual view class defined as a UIView subclass, not the context.

But I’m guessing you must already have addons/ofxiPhone/src stuff already in your project to have got this far, right…?

Well, I have the ofxiPhone addon as part of my oF build. I thought that means I don’t have to explicitly include it? I tried adding ofxiPhone to my project and importing EAGLView.h, but now I have a ton of errors- I think because the whole addon is being compiled twice.

Hi Gwydion,

Will you be able to share an example project with the basic setup for gestures?

Thanks in advance

  • rS

I would also appreciate an example as all other forum posts send me here, but with no answers at all. just the sample code i need to add to testApp.h and testApp.mm would be valuable, then i can utilize the various variables…(in my case, the scaling variable from pinching).

The problem is @selector can only ‘select’ class methods of obj-c classes. So to utilize the UIGestureRecognizer classes, we need to make a small wrapper. Like this:

ofTapGestureRecognizer.h

  
  
#import <Foundation/Foundation.h>  
  
  
@interface ofTapGestureRecognizer : NSObject {  
    UITapGestureRecognizer *tapGestureRecognizer;  
}  
  
-(id)initWithView:(UIView*)view;  
-(void)handleTap:(UITapGestureRecognizer *) gr;  
  
@end  
  

ofTapGestureRecognizer.m

  
  
#import "ofTapGestureRecognizer.h"  
  
@implementation ofTapGestureRecognizer  
  
-(id)initWithView:(UIView*)view{  
	if((self = [super init])){  
		tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];  
		[view addGestureRecognizer:tapGestureRecognizer];  
	}  
	return self;  
}  
  
-(void)handleTap:(UITapGestureRecognizer *) gr{  
	if([gr state] == UIGestureRecognizerStateRecognized){  
		NSLog(@"tap!");  
	}  
}  
  
-(void)dealloc{  
	[tapGestureRecognizer release];  
	[super dealloc];  
}  
  
@end  
  

Put this in testApp.h

  
ofTapGestureRecognizer *recog;  

And this in setup() of testApp.mm

  
  
EAGLView *view = ofxiPhoneGetGLView();  
recog = [[ofTapGestureRecognizer alloc] initWithView:view];  
  

Don’t forget to call [recog release]; when you’re done with it.

Hello,
I try to use swipe gesture recognition, I looked at daanvanhasselt suggestion and rjraffa addons https://github.com/rjraffa/ofxGestureRecognizer
This is what I have to recognize all swipe:

  
#import "ofSwipeGestureRecognizer.h"  
  
@implementation ofSwipeGestureRecognizer  
  
-(id)initWithView:(UIView*)view{  
    if((self = [super init])){  
        swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];  
        [swipeGestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];  
        [view addGestureRecognizer:swipeGestureRecognizer];  
          
        swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];  
        [swipeGestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];  
        [view addGestureRecognizer:swipeGestureRecognizer];  
          
          
        swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];  
        [swipeGestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];  
        [view addGestureRecognizer:swipeGestureRecognizer];  
          
          
        swipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];  
        [swipeGestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];  
        [view addGestureRecognizer:swipeGestureRecognizer];  
          
          
          
        swipeGestureRecognizer.cancelsTouchesInView = NO;  
  
    }  
    return self;  
}  
  
-(void)handleSwipe:(UISwipeGestureRecognizer *) gr{  
      
      
    NSLog(@"%d",[gr state]);  
    
   if([gr state] == UIGestureRecognizerStateEnded){  
        swiped=true;  
      if (gr.direction == UISwipeGestureRecognizerDirectionUp) {  
        NSLog(@"Swipe up!");  
        direction=1;  
    }  
    else if (gr.direction == UISwipeGestureRecognizerDirectionDown){  
        NSLog(@"Swipe down!");  
        direction=2;  
    }  
    else if (gr.direction == UISwipeGestureRecognizerDirectionLeft){  
        NSLog(@"Swipe left!");  
        direction=3;  
    }  
    else if (gr.direction == UISwipeGestureRecognizerDirectionRight){  
        NSLog(@"Swipe right!");  
        direction=4;  
    }  
  
    
    }  
      
      
}  
  
-(void)dealloc{  
    [swipeGestureRecognizer release];  
    [super dealloc];  
}  
  
@end  

This is not working how I would expect:
The swipe is recognized even when the finger is not up yet. A touchUp in testApp is triggered when the finger is still moving and down.
I need somehow the swipe to be ignored if there is a pan.
Am I missing something?

Thank you