UIView Gesture
iOS covers several gestures in device.
We can handle these gestures using UIXxxGesturerecognizer family.
List
This is the list of UIxxxGestureRecognizer family.
Gesture | Class |
---|---|
Tap(single, double, more…) | UITapGestureRecognizer |
Pinch | UIPinchGestureRecognizer |
Pan | UIPanGestureRecognizer |
Swipe | UISwipeGestureRecognizer |
Rotate | UIRotationGestureRecognizer |
Long Press | UILongPressGestureRecognizer |
Sample
This sample is to attach gesture class to small UIView.
GestureTestViewController.h
@interface GestureTestViewController : UIViewController @end
GestureTestViewController.m
@interface GestureTestViewController () @property (nonatomic) UIView *testView; @end @implementation GestureTestViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.testView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)]; [self.testView setBackgroundColor:[UIColor greenColor]]; //[self singleTap]; //[self doubleTap]; // Test double click in emulator //[self pan]; //[self swipe]; //[self rotate]; [self longPress]; [self.view addSubview:self.testView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Events -(void)singleTap { UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)]; [self.testView addGestureRecognizer:tap]; } -(void)doubleTap { UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapDoubleGesture:)]; doubleTap.numberOfTapsRequired = 2; [self.testView addGestureRecognizer:doubleTap]; } -(void)pinch { UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)]; [self.testView addGestureRecognizer:pinchGesture]; } -(void)pan { UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)]; [self.testView addGestureRecognizer:panGesture]; } -(void)swipe { // Left UISwipeGestureRecognizer* swipeLeftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeLeftGesture:)]; swipeLeftGesture.direction = UISwipeGestureRecognizerDirectionLeft; [self.testView addGestureRecognizer:swipeLeftGesture]; // Right UISwipeGestureRecognizer* swipeRightGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRightGesture:)]; swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight; [self.view addGestureRecognizer:swipeRightGesture]; } -(void)rotate { UIRotationGestureRecognizer* rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotationGesture:)]; [self.view addGestureRecognizer:rotationGesture]; } -(void)longPress { UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGesture:)]; // Set minimum duration //longPressGesture.minimumPressDuration = 3; [self.view addGestureRecognizer:longPressGesture]; } #pragma mark - #pragma mark - Gesture Handler -(void)handleTapGesture:(UITapGestureRecognizer *)sender { NSLog(@"Single Tap"); } -(void)handleTapDoubleGesture:(UITapGestureRecognizer *)sender { NSLog(@"Double Tap"); } -(void)handlePinchGesture:(UIPinchGestureRecognizer *)sender { NSLog(@"Pinch"); UIPinchGestureRecognizer* pinch = (UIPinchGestureRecognizer*)sender; NSLog(@"pinch scale=%f, velocity=%f", pinch.scale, pinch.velocity); // scale, velocity } -(void)handlePanGesture:(UIPanGestureRecognizer *)sender { UIPanGestureRecognizer* pan = (UIPanGestureRecognizer*) sender; CGPoint location = [pan translationInView:self.testView]; NSLog(@"pan x=%f, y=%f", location.x, location.y); // After transition } -(void)handleSwipeLeftGesture:(UISwipeGestureRecognizer *)sender { NSLog(@"Left swipe"); } -(void)handleSwipeRightGesture:(UISwipeGestureRecognizer *)sender { NSLog(@"Right swipe"); } -(void)handleRotationGesture:(UIRotationGestureRecognizer *)sender { UIRotationGestureRecognizer* rotation = (UIRotationGestureRecognizer*) sender; NSLog(@"rotation rad=%f, velocity=%f", rotation.rotation, rotation.velocity); } -(void)handleLongPressGesture:(UILongPressGestureRecognizer *)sender { NSLog(@"Long Press"); } @end
Result
Display only green color UIVIew. You can do any action you set in this UIView.
How to test?
Real device is better to test gesture sample.
But, we can do it in simulator to some extent.
This is nice example to test iOS simulator testing.
How to Make Gestures on the iOS Simulator
Clickable setting
testView setUserInteractionEnable:YES];