Reputation: 83
I know that I can use requireGestureRecognizerToFail
function to distinguish single tap and double tap, but I meet a tiny problem, and I want to fix it. My code is as below:
- (IBAction)singleTap:(UITapGestureRecognizer *)sender {
NSLOGD_METADATAONLY();
hideNavigationBar();
hideStatusBar();
[sender requireGestureRecognizerToFail:self.doubleTapRecognizer];
}
- (IBAction)doubleTap:(UITapGestureRecognizer *)sender {
NSLOGD_METADATAONLY();
//TODO
}
When I open a file and double tap(action1), single tap(result1) will be called firstly and then double tap(result2) will be called.
But if I open a file and single tap(action3), then double tap(action4), the result of action4 is working well,single tap will not be called only double tap will be called. I guess it is because in action3 the function requireGestureRecognizerToFail
is called.
My question is HOW CAN I make action1 just call result2 and not call result1?
Upvotes: 1
Views: 751
Reputation: 16190
Update your code with [singleTapRecognizer requireGestureRecognizerToFail: doubleTapRecognizer]
when you creating UITapGestureRecognizer
Example:
// single tap
UITapGestureRecognizer *singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: @selector(handleSingleTapOnView:)];
[singleTapRecognizer setNumberOfTouchesRequired:1];
[singleTapRecognizer requireGestureRecognizerToFail:doubleTapRecognizer];
[view addGestureRecognizer: singleTapRecognizer];
// double tap
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget: tableViewController action: @selector (handleDoubleTapOnView:)];
[doubleTapRecognizer setNumberOfTouchesRequired:2];
[view addGestureRecognizer: doubleTapRecognizer];
When you set requireGestureRecognizerToFail:
for a gesture recognizer, you're saying that it should only recognize the gesture if the other gesture recognizer did not.
Upvotes: 0
Reputation: 2751
Extend your class as UIGestureRecognizerDelegate
.
class ViewController: UIViewController, UIGestureRecognizerDelegate {
Then assign your view controller as delegate of gesture recognizer.
tapGesture .delegate = self;
doubleTapGesture .delegate = self;
Implementation of shouldRequireFailureOf
prevents the one gesture from being recognized until after the other gesture recognizer explicitly reaches the failed state.
You need to recognize a single tap only after the failure of a double tap, which happens when the touch sequence contains only one tap. This can be achieved implementing following.
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer,
shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
// Don't recognize a single tap until a double-tap fails.
if gestureRecognizer == self.tapGesture &&
otherGestureRecognizer == self.doubleTapGesture {
return true
}
return false
}
Hope it helps.Happy Coding!!
Upvotes: 0
Reputation: 285
Write the below lines in ViewDidLoad
[singleTap requireGestureRecognizerToFail:doubleTap];
Upvotes: 1