12 May 2018

UITapGetureRecognizer, UIPanGestureRecognizer, UIRotationGestureRecognizer in IOS - Part 1



You touch screen of your smartphones, in technical terms it is known as gesture. There are different types of gesture API's available to IOS developers

UITapGetureRecognizer
UIPanGestureRecognizer
UIRotationGestureRecognizer

In this post, we will cover UITapGestureRecognizer from above listed API's and rest of the two will be covered in next article/post. Let us briefly understand when to these API's and in which cases they are needed.

UITapGetureRecognizer: UITapGestureRecognizer is a concrete subclass of UIGestureRecognizer that looks for single or multiple taps. For the gesture to be recognized, the specified number of fingers must tap the view a specified number of times.

UIPanGestureRecognizer: UIPanGestureRecognizer is a concrete subclass of UIGestureRecognizer that looks for panning (dragging) gestures. The user must be pressing one or more fingers on a view while they pan it. Clients implementing the action method for this gesture recognizer can ask it for the current translation and velocity of the gesture.

UIRotationGestureRecognizer: UIRotationGestureRecognizer is a concrete subclass of UIGestureRecognizer that looks for rotation gestures involving two touches. When the user moves the fingers opposite each other in a circular motion, the underlying view should rotate in a corresponding direction and speed.

Now all three gestures are cleared to you and its time to know how we can use them programmatically.

UITapGetureRecognizer

We can use UITapGetureReconizer API to detect when user taps on screen with his finger. For this, we simply add our gestureRecognizer object to a view if we want to detect tap on whole view or screen then we will add it to self.view otherwise we will add it to particular view using method addGestureRecognizer

Let us create a project named HowToUseTapGesture, Open your ViewController.m file and in your ViewDidLoad method add below code

 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAtPoint:)];
 [self.view addGestureRecognizer:tapGesture];
 tapGesture.numberOfTapsRequired=1;

 [tapGesture release];
 tapGesture = nil;


UITapGesture Code

In above code, we created an object of UITapGestureReconizer, allocate it with target  and gave him selector which will be gets called when user tap on screen of the device. Then we add this gesture to our main view as we want to detect tap on whole screen.We have to give number of taps we want to detect before firing our selector, so we gave 1 to gesture property named as numberOfTapsRequired. So, now our selector will gets called when user tap or touch the device screen.Lastly we release our gesture object and set it to nil. If using ARC then last two lines of code are not required.

Finally we implement our selector.

-(void)tapAtPoint:(UITapGestureRecognizer *)tapgesture
{

    NSLog(@"Tap detected");
}



Imaplementing Gesture selector


Run your project and see in console when you tap your simulator screen or device screen.


OutPut

 




Source Code: Download above source code from here

Video Output:


                       

No comments:

Post a Comment