Problem with UITapGestureRecognizer for double tap
Asked Answered
G

2

8

I've two UITapGestureRecognizer: singleTap and doubleTap initialized with two different actions.

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
[singleTap requireGestureRecognizerToFail:doubleTap];
[doubleTap setNumberOfTapsRequired:2];
[imageView addGestureRecognizer:doubleTap];
[imageView addGestureRecognizer:singleTap];

When I run my app in simulator the single tap responds correctly but not the double tap ! When I double clicks nothings happens, I suppose iOS dose recognize the double tap because the action of single tap doesn't being called (due to [singleTap requireGestureRecognizerToFail:doubleTap];), but I can't understand why doesn't it do the action handleDoubleTap.

Grown answered 10/5, 2011 at 14:30 Comment(4)
Does it work if you remove the single tap gesture recognizer?Caelum
Nop... I've just changed setNumberOfTapsREquired to 3 for doubleTap, and when I double click nothing happens neither ! It's stranger since in this case a double click should be recognized as a single click, right ? Double click issue with the iOS simulator perhaps ?Grown
Never trust the simulator, I would test this on an actual device.Caelum
Try here #9009475Campball
Q
6

I think the problem is that UIImageView and UILabel both override the default value of YES for the userInteractionEnabled property, and sets it to NO.

Add imageView.userInteractionEnabled = YES; and try again.

Quadrivalent answered 10/5, 2011 at 18:23 Comment(0)
L
6

The following code works for me:

- (void)handleTap:(UIGestureRecognizer*)gr {
    NSLog(@"----------------- tap ----------------");
}
- (void)handleDoubleTap:(UIGestureRecognizer*)gr {
    NSLog(@"================= double tap ============");
}
- (XXXView*)createXXXView {
    XXXView *view = [[[XXXView alloc] init] autorelease];
    view.xxx=...;//irrelevant

    UITapGestureRecognizer *dtr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    dtr.numberOfTapsRequired = 2;

    UIGestureRecognizer *tr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];

    [tr requireGestureRecognizerToFail:dtr];
    [view addGestureRecognizer:tr];
    [view addGestureRecognizer:dtr];

    return view;
}
Lothar answered 28/7, 2011 at 17:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.