Introductions:
UIGestureRecognizer is an abstract base class for concrete gesture-recognizer classes.
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 the old days before UIGestureRecognizers, if you wanted to detect a gesture such as a swipe, you’d have to register for notifications on every touch within a UIView – such as touchesBegan, touchesMoves, and touchesEnded.
The concrete subclasses of UIGestureRecognizer are the following:
UITapGestureRecognizer
UIPinchGestureRecognizer
UIRotationGestureRecognizer
UISwipeGestureRecognizer
UIPanGestureRecognizer
UILongPressGestunizer
A gesture recognizer has one or more target-action pairs associated with it. If there are multiple target-action pairs, they are discrete, and not cumulative. Recognition of a gesture results in the dispatch of an action message to a target for each of those pairs. The action methods invoked must conform to one of the following signatures:
-(void)handleGesture;
-(void)handleGesture:(UIGestureRecognizer *)gestureRecognize;
Solution :
Step 1 : Create a new file of class ViewController and a UIView
Step 2 : in viewDidLoad implement concrete subclasses of UIGestureRecognizer
eg :
UITapGestureRecognizer
-(void) viewDidLoad {
UITapGestureRecognizer * recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
recognizer.delegate = self;
[view addGestureRecognizer:recognizer];
}
Step 3 : Implement the gesture handle
- (void)handleTap:(UITapGestureRecognizer *)recognizer {
NSLog (@”your implementation here”);
}
Conclusion :
UIGestureRecognizer classes! These provide a default implementation of detecting common gestures such as taps, pinches, rotations, swipes, pans, and long presses. By using them, not only it reduce you code length, but it easy too.