Gesture Recognizer code and source files

Gesture Recognizer code and source files
2011-05-09 19:18

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;

@end

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  {

NSLog(@”Swiped”);

}

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

NSLog(@”Long Press”);

}

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

NSLog(@”Pan”);

}

-(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  {

NSLog(@”Pinch”);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值