/Single-finger click
2: UITapGestureRecognizer * singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget: self
3: action: @ selector (handleSingleFingerEvent :)];
4: singleFingerOne. numberOfTouchesRequired = 1; // hand index
5: singleFingerOne. numberOfTapsRequired = 1; // The number of times the tap operation is performed.
6: singleFingerOne. delegate = self;
7:
8: // double-click
9: UITapGestureRecognizer * singleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget: self
10: action: @ selector (handleSingleFingerEvent :)];
11: singleFingerTwo. numberOfTouchesRequired = 1;
12: singleFingerTwo. numberOfTapsRequired = 2;
13: singleFingerTwo. delegate = self;
14:
15: // double-finger click
16: UITapGestureRecognizer * doubleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget: self
17: action: @ selector (handleDoubleFingerEvent :)];
18: doubleFingerOne. numberOfTouchesRequired = 2;
19: doubleFingerOne. numberOfTapsRequired = 1;
20: doubleFingerOne. delegate = self;
21:
22: UITapGestureRecognizer * doubleFingerTwo = [[UITapGestureRecognizer alloc] initWithTarget: self
23: action: @ selector (handleDoubleFingerEvent :)];
24: doubleFingerTwo. numberOfTouchesRequired = 2;
25: doubleFingerTwo. numberOfTapsRequired = 2;
26: doubleFingerTwo. delegate = self;
27:
28: // if the following is not added, when the single-finger double-click operation is performed, the single-finger click process is called first, and then the single-finger double-click process is called.
29: [singleFingerOne requireGestureRecognizerToFail: singleFingerTwo];
30: // likewise, this is also true for dual fingers.
31: [doubleFingerOne requireGestureRecognizerToFail: doubleFingerTwo];
32:
33: [self. view addGestureRecognizer: singleFingerOne];
34: [self. view addGestureRecognizer: singleFingerTwo];
35: [self. view addGestureRecognizer: doubleFingerOne];
36: [self. view addGestureRecognizer: doubleFingerTwo];
37:
38: [singleFingerOne release];
39: [singleFingerTwo release];
40: [doubleFingerOne release];
41: [doubleFingerTwo release];
The code for event handling:
1: // process a single-finger event
2:-(void) handleSingleFingerEvent :( UITapGestureRecognizer *) sender
3 :{
4: if (sender. numberOfTapsRequired = 1 ){
5: // single-finger click
6: NSLog (@ "Single-finger click ");
7:} else if (sender. numberOfTapsRequired = 2 ){
8: // double-click
9: NSLog (@ "double-click ");
10 :}
11 :}
12: // process dual-finger events
13:-(void) handleDoubleFingerEvent :( UITapGestureRecognizer *) sender
14 :{
15: if (sender. numberOfTapsRequired = 1 ){
16: // double-finger click
17: NSLog (@ "double-finger click ");
18:} else if (sender. numberOfTapsRequired = 2 ){
19: // double-click
20: NSLog (@ "double-click ");
21 :}
22 :}
Copy the corresponding code to your project and you will be able to use it. Because the Code has detailed explanations, we will not repeat the explanations here.
The Code only lists the single-finger and double-finger processing for click or multi-click operations. Similarly, the numberOfTouchesRequired attribute must be modified for multi-finger operations, and the numberOfTapsRequired attribute must be modified for the number of clicks.
Other gestures such as UISwipeGestureRecognizer, UILongPressGestureRecognizer, and UILongPressGestureRecognizer use similar processing.