IOS. UI advanced. UITouch touch with UIGesture gesture. 03. gesture reader (UIGestureRecognizer), uigesture.03
1. What is a gesture reader:
The UIGestureRecognizer is used to identify whether a user uses a device, such as a gesture, a slide, a tap, or something else.
UIGestureRecognizer class, used to detect and recognize gestures used by users when using devices. It is an abstract class that defines the basic behavior of all gestures. The following is a subclass of UIGestureRecognizer.
Specific user gestures:
- UITapGestureRecognizer (click)
- UIPinchGestureRecognizer (kneading)
- UISwipeGestureRecognizer (sweep)
- UIRotationGestureRecognizer (rotate)
- UILongPressGestureRecognizer (long press)
They all inherit from UIGestureRecognizer and are its subclasses.
Ii. Instances
Take UITapGestureRecognizer as an example:
Let's first look at its two attributes:
@ Property (nonatomic) NSUInteger numberOfTapsRequired; // Default is 1. The number of taps required to match
@ Property (nonatomic) NSUInteger numberOfTouchesRequired; // Default is 1. The number of fingers required to match
The number of times the numberOfTapsRequired attribute can be tapped. The default value is one. The numberOfTouchesRequired attribute allows you to set a few finger clicks. The default value is one finger. The two attributes can be used together to achieve different effects.
Sample Code:
-(Void) viewDidLoad {
[Super viewDidLoad];
// Add a gesture gesture1 and implement the method gestureAction
UITapGestureRecognizer * gesture1 = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (sigleGestureAction)];
[Self. view addGestureRecognizer: gesture]; // do not forget to add a gesture. Here, the gesture is added to self. view.
// Add a double-click gesture to achieve double-click.
UITapGestureRecognizer * gesture2 = [[UITapGestureRecognizer alloc] initWithTarget: self action: @ selector (doubelGestureAction)];
Gesture2.numberOfTapsRequired = 2; // if you double-click it, set this attribute value to OH 2.
[Self. view addGestureRecognizer: gesture2];
Gesture1 requireGestureRecognizerToFail: gesture2]; // note that this code separates the two gestures.
}
-(Void) sigleGestureAction {
NSLog (@ "click ");
}
-(Void) doubelGestureAction {
NSLog (@ "double-click ");
}
The methods for adding other gestures are basically the same. When you add an object, you can check the document, set the attributes, and write down the added method.
For iOS beginners, if any errors occur, please kindly advise.