Gesture Recognizer code and source files

Sometimes (like all the time) I forget to actually post some useful code on this blog. Well I was just pondering the epic usefulness of these relatively new Gestures Recognizers you can use in your iPhone or iPad apps, so here’s some code for ya (sample files) or you can read the meat of it below. Use it and you’ll have a…

  • UITapGestureRecognizer (detect taps, either with one or more fingers, and one or more taps)
  • UISwipeGestureRecognizer (detect swipes, either with one or more fingers, swiping Up, Down, Right or Left)
  • UILongPressGestureRecognizer (detects a long press, the example detects a press of 3 seconds)
  • UIPanGestureRecognizer (detects panning)
  • UIRotationGestureRecognizer (detects fingers moving in a rotation)
  • UIPinchGestureRecognizer (detects pinching either inward or outward)

For your header file ( in this case, the file GesturesViewController.h )…

#import <UIKit/UIKit.h>

@interface GesturesViewController : UIViewController <UIGestureRecognizerDelegate> {

UITapGestureRecognizer *tapGR;

UISwipeGestureRecognizer *swipeUpGR;

UILongPressGestureRecognizer *longPressGR;

UIPanGestureRecognizer *panGR;

UIRotationGestureRecognizer *rotationGR;

UIPinchGestureRecognizer *pinchGR;


@property (nonatomic, retain) UITapGestureRecognizer *tapGR;

@property (nonatomic, retain) UISwipeGestureRecognizer *swipeUpGR;

@property (nonatomic, retain) UILongPressGestureRecognizer *longPressGR;

@property (nonatomic, retain) UIPanGestureRecognizer *panGR;

@property (nonatomic, retain) UIRotationGestureRecognizer *rotationGR;

@property (nonatomic, retain) UIPinchGestureRecognizer *pinchGR;


For your implementation file ( in this case, the file GesturesViewController.m )

#import “GesturesViewController.h”

@implementation GesturesViewController

@synthesize tapGR, swipeUpGR, longPressGR, panGR, rotationGR, pinchGR;

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.

– (void)viewDidLoad {

[super viewDidLoad];

UIGestureRecognizer *recognizer;

// taps

recognizer = [[ UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];

tapGR = (UITapGestureRecognizer *)recognizer;

tapGR.numberOfTapsRequired = 3;

tapGR.numberOfTouchesRequired = 1;

[self.view addGestureRecognizer:tapGR];

[recognizer release];

//swipe up

recognizer = [[ UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

swipeUpGR = (UISwipeGestureRecognizer *)recognizer;

swipeUpGR.numberOfTouchesRequired = 1;

swipeUpGR.direction = UISwipeGestureRecognizerDirectionUp;  //change Up to Right, Left or down

[self.view addGestureRecognizer:swipeUpGR];

[recognizer release];

// long tap

recognizer = [[ UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];

longPressGR = (UILongPressGestureRecognizer *)recognizer;

longPressGR.minimumPressDuration = 3.0;

[self.view addGestureRecognizer:longPressGR];

[recognizer release];

// pan gr

recognizer = [[ UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];

panGR = (UIPanGestureRecognizer *)recognizer;

panGR.minimumNumberOfTouches = 2;

panGR.maximumNumberOfTouches = 6;

[self.view addGestureRecognizer:panGR];

[recognizer release];

// rotation

recognizer = [[ UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotation:)];

rotationGR = (UIRotationGestureRecognizer *)recognizer;

[self.view addGestureRecognizer:rotationGR];

[recognizer release];

// pinch gr

recognizer = [[ UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];

pinchGR = (UIPinchGestureRecognizer *)recognizer;

[self.view addGestureRecognizer:pinchGR];

[recognizer release];


-(void) handleTap:(UITapGestureRecognizer *)recognizer  {

NSLog(@”3 taps”);


-(void) handleSwipe:(UISwipeGestureRecognizer *)recognizer  {



-(void) handleLongPress:(UILongPressGestureRecognizer *)recognizer  {

NSLog(@”Long Press”);


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



-(void) handleRotation:(UIRotationGestureRecognizer *)recognizer  {

/* Get the rotation angle in degrees */

float RotationinDegrees = recognizer.rotation * (180/M_PI);

/* Print it to the debug console */

NSLog(@”%f”, RotationinDegrees);


-(void) handlePinch:(UIPinchGestureRecognizer *)recognizer  {




4 thoughts on “Gesture Recognizer code and source files

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s