THIS CONTENT DOWNLOAD SHORTLY

Objective

The main objective of this post is to describe how gesture recognizer works in iOS.

 

Introduction

If you need to detect gestures in your app, such as taps, pinches, pans, or rotations, it’s extremely easy with the built-in UIGestureRecognizer classes. In this post, I’ll show you how you can easily add gesture recognizers into your app, both within the Storyboard editor and programatically.

We’ll create a simple app where you can move bluebox dragging, pinching, and rotating with the help of gesture recognizers. Following are the steps that give you a brief introduction about how to recognize different gestures in your application.

 

Step 1 Create Xcode Project

Open up Xcode and create a new project with the iOS/ Application/ Single View Application template. For the Product Name enter GestureDemoExample, for the Device Family choose iPhone, and select the Use Storyboard and Use Automatic Reference Counting checkboxes.

 

Step 2 Design UI

Next, open up MainStoryboard.storyboard, and drag an View into the View Controller.

 

Step 3 Create IBOutlet

Create property in ViewController.h file as per following:

@property (weak, nonatomic) IBOutlet UIView *blueBox;
 

Step 4 Put Gesture methods

 

4.1 UIPanGestureRecognizer

ui pan gesture recognizer

Add the following implementations in ViewController.m

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:1];
[blueBox addGestureRecognizer:panGesture];
panGesture = nil;

Method to handle pan gesture:

-(void)handlePanGesture:(id)sender
{
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) {
firstX = [[sender view] center].x;
firstY = [[sender view] center].y;
}
translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);
[[sender view] setCenter:translatedPoint];
}
 

4.2 UIPinchGestureRecognizer

ui pinch gesture recognizer

Add the following implementations in ViewController.m

UIRotationGestureRecognizer *rotate=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[blueBox addGestureRecognizer:rotate];

Method to handle pinch gesture:

-(void) rotation:(UIRotationGestureRecognizer *) sender
{
if ([sender state] == UIGestureRecognizerStateBegan || [sender state] == UIGestureRecognizerStateChanged) {
[sender view].transform = CGAffineTransformRotate([[sender view] transform], [(UIRotationGestureRecognizer *)sender rotation]);
[(UIRotationGestureRecognizer *)sender setRotation:0];}
}
 

4.3 UIRotationGestureRecognizer

ui rotation gesture recognizer

Add the following implementations in ViewController.m

UIRotationGestureRecognizer *rotate=[[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotation:)];
[blueBox addGestureRecognizer:rotate];

Method to handle rotation gesture:

-(void) rotation:(UIRotationGestureRecognizer *) sender
{
if ([sender state] == UIGestureRecognizerStateBegan || [sender state] == UIGestureRecognizerStateChanged) {
[sender view].transform = CGAffineTransformRotate([[sender view] transform], [(UIRotationGestureRecognizer *)sender rotation]);
[(UIRotationGestureRecognizer *)sender setRotation:0];}
}
 

4.4 UITapGestureRecognizer

gesturerecognizerexample

Add the following implementations in ViewController.m

UITapGestureRecognizer *tapper=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapped:)]; [blueBox addGestureRecognizer:tapper];

Method to handle tap gesture:

-(void) tapped:(UIGestureRecognizer *) sender
{
CGFloat r=(CGFloat) random()/(CGFloat) RAND_MAX;
CGFloat g=(CGFloat) random()/(CGFloat) RAND_MAX;
CGFloat b=(CGFloat) random()/(CGFloat) RAND_MAX;
blueBox.backgroundColor=[UIColor colorWithRed:r green:g blue:b alpha:1];
}
 

4.5 UISwipeGestureRecognizer

ui swipe gesture recognizer

Add the following implementations in ViewController.m

UISwipeGestureRecognizer *swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swiping:)];
[blueBox addGestureRecognizer:swipe];

Method to handle swipe gesture:

-(void) swiping:(UISwipeGestureRecognizer *) sender
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationRepeatAutoreverses:YES];
[UIView setAnimationRepeatCount:5];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(stop:)];
blueBox.backgroundColor=[UIColor whiteColor];
[UIView commitAnimations];
}
-(void) stop: sender
{
blueBox.backgroundColor=[UIColor blueColor];
}

UITapGestureRecognizer:

ui tap gesture recognizer

Add the following implementations in ViewController.m

UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressed:)];
longPress.minimumPressDuration=3;

Method to handle long press gesture:

-(void) longPressed:(UIGestureRecognizer *) sender
{
CGFloat r=(CGFloat) random()/(CGFloat) RAND_MAX;
CGFloat g=(CGFloat) random()/(CGFloat) RAND_MAX;
CGFloat b=(CGFloat) random()/(CGFloat) RAND_MAX;
blueBox.backgroundColor=[UIColor colorWithRed:r green:g blue:b alpha:1];
}

I hope you found this blog helpful while working Gesture Recognizer in iOS. Let me know if you have any questions or concerns regarding iOS, please put a comment here and we will get back to you ASAP.

Got an Idea of iPhone App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best iPhone App Development Company in India.

I am iOS developer, as a developer my basic goal is continue to learn and improve my development skills , to make application more user friendly.