The practice of recognizing double-click events in iOS:
- Create the UITapGestureRecognizer object, set the value of numberoftapsrequired to 2,
UITapGestureRecognizer *doubletaprecognizer = [[UITapGestureRecognizer alloc] initwithtarget:self Action: @selector (Doubletap:)]; 2 ; [Self addgesturerecognizer:doubletaprecognizer];
- Action function
-(void) Doubletap: (Uigesturerecognizer *) gr { NSLog (@ "recognized Double TAP "); }
The Touchesbegan function is implemented in the same uiview
// Touch Start -(void) Touchesbegan: (Nsset *) touches withevent: (uievent *)event{ NSLog (@ "touch begin"); }
problem
When you double-click, view the log. The order in which the touch events occurred is found below:
2015-09-15 21:57:55.249 touchtracker[20387:5622651] Touch begin
2015-09-15 21:57:55.421 touchtracker[20387:5622651] Double tap
This is because when the user taps the screen, UIView receives the message in Touchesbegan:withevent. UIView receives the touchesbegan:withevent message before the Uigesturerecognizer recognizes the double-click Gesture, and after the recognition Uigesturerecognizer recognizes the double-tap gesture, Uigesturerecognizer handles related touch events on its own, and uiresponder messages caused by touch events are no longer sent to UIView. UIView will not receive Uiresponder messages until Uigesturerecognizer detects that the tap gesture has ended.
Solve
you need to avoid sending touchbegin:withevent messages to UIView before you recognize a tap gesture. the Delaystouchesbegan value is set to Yes.
UITapGestureRecognizer * doubletaprecognizer= [[UITapGestureRecognizer alloc]initwithtarget:self action:@ Selector (DOUBLETAP:)]; doubletaprecognizer.numberoftapsrequired=2; Doubletaprecognizer.delaystouchesbegan=YES; [Self addgesturerecognizer:doubletaprecognizer];
difference Double click and click
Add Single click Recognition
// Single tap *taprecognizer= [[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector (tap:)] ; [Self addgesturerecognizer:taprecognizer];action function -(void) Tap: (Uigesturerecognizer *) gr{ NSLog (@ "tap");}
When I looked at the log output, I found that when I clicked two times, UIView didn't recognize the Click event and the double click event. Tap: And Doubletap: Will execute.
2015-09-15 22:36:02.441 touchtracker[20631:5667414] Tap
2015-09-15 22:36:02.600 touchtracker[20631:5667414] Double tap
Because the double-click event contains two clicks, the first click is recognized as a click event.
Workaround
Settings are temporarily not recognized after a click (a little pause) until you identify the click event after the event is not double-clicked.
// Single tap *taprecognizer= [[UITapGestureRecognizer alloc]initwithtarget:self Action: @selector (tap:)] ; [Taprecognizer Requiregesturerecognizertofail:doubletaprecognizer]; [Self addgesturerecognizer:taprecognizer];
uiview-the difference between double-tap gestures and click gestures