1 Preface
Gestures are actually a combination of touch events. gesture event recognition must be added to a UIView class. Multiple gesture identifiers can be added to a single view. Once some gesture actions are captured on this interface, this view will pass the gesture action to other gesture identifiers.
Some touch events require support from the mobile phone system. The following are six gesture identifiers provided by iOS SDK5.
• Swipe // stroke
• Rotation // Rotation
• Pinch // contraction
• Pan // shake
• Long press // Long press
• Tap // click
The most basic framework must follow the steps below to process gestures:
Then create an object for the appropriate gesture reader.
The observer binds the object of the gesture reader to a view.
Add some methods to capture gesture events.
• The return type of this method must be null.
• This method either has no parameter type or can only accept one UIGestureRecognizer type parameter.
Gesture Recognition devices can be divided into two categories: one is a separate gesture and the other is a coherent gesture combination. As the name suggests, a gesture is followed by an event that is captured by a listener and then executed accordingly. Consistent is a group of gesture actions, and then listens to capture events for relevant processing. . Consistent is a group of gesture actions, and then listens to capture events for relevant processing.
Reference Code
-(Void) tapRecognizer :( UITapGestureRecognizer *) paramSender {
/**/
}
-(Void) tapRecognizer {
/**/
}
In this section, we will learn about UISwipeGestureRecognizer.
2. code example
ZYViewController. m
[Plain]
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/* Instantiate the gesture object */
Self. swipeGestureRecognizer = [UISwipeGestureRecognizer alloc]
InitWithTarget: self action: @ selector (handleSwipes :)];
/* Slide from right to left */
Self. swipeGestureRecognizer. direction = UISwipeGestureRecognizerDirectionLeft;
/* Single finger */
Self. swipeGestureRecognizer. numberOfTouchesRequired = 1;
/* Add to view */
[Self. view addGestureRecognizer: self. swipeGestureRecognizer];
}
-(Void) handleSwipes :( UISwipeGestureRecognizer *) paramSender {
// Press
If (paramSender. direction & UISwipeGestureRecognizerDirectionDown ){
NSLog (@ "Swiped Down .");
}
// Left
If (paramSender. direction & UISwipeGestureRecognizerDirectionLeft ){
NSLog (@ "Swiped Left .");
}
// Right
If (paramSender. direction & UISwipeGestureRecognizerDirectionRight ){
NSLog (@ "Swiped Right .");
}
// Lift
If (paramSender. direction & UISwipeGestureRecognizerDirectionUp ){
NSLog (@ "Swiped Up .");
}
}
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
/* Instantiate the gesture object */
Self. swipeGestureRecognizer = [UISwipeGestureRecognizer alloc]
InitWithTarget: self action: @ selector (handleSwipes :)];
/* Slide from right to left */
Self. swipeGestureRecognizer. direction = UISwipeGestureRecognizerDirectionLeft;
/* Single finger */
Self. swipeGestureRecognizer. numberOfTouchesRequired = 1;
/* Add to view */
[Self. view addGestureRecognizer: self. swipeGestureRecognizer];
}
-(Void) handleSwipes :( UISwipeGestureRecognizer *) paramSender {
// Press
If (paramSender. direction & UISwipeGestureRecognizerDirectionDown ){
NSLog (@ "Swiped Down .");
}
// Left
If (paramSender. direction & UISwipeGestureRecognizerDirectionLeft ){
NSLog (@ "Swiped Left .");
}
// Right
If (paramSender. direction & UISwipeGestureRecognizerDirectionRight ){
NSLog (@ "Swiped Right .");
}
// Lift
If (paramSender. direction & UISwipeGestureRecognizerDirectionUp ){
NSLog (@ "Swiped Up .");
}
}
Running result
Result displayed on the console after you move the cursor from right to left on the simulator Screen
22:22:43. 005 UISwipeGestureTest [734: c07] Swiped Left.