Search code examples
iosobjective-cuiscrollviewgestureuitapgesturerecognizer

Multiple UITapGestureRecognizer not working on UIScrollView


I want to add multiple UITapGestureRecognizer on UIScrollView but it recognise only one gesture.
I want to add first gesture for touch begin and second one for touch end event.

Following is my code:-

self.tapStartGesture = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapGesture:)];
self.tapStartGesture.numberOfTapsRequired = 1;
self.tapStartGesture.numberOfTouchesRequired = 1;
[self.tapStartGesture setState:UIGestureRecognizerStateBegan];
[self.scrollView addGestureRecognizer:self.tapStartGesture];

self.tapEndGesture = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapGesture:)];
self.tapEndGesture.numberOfTapsRequired = 1;
self.tapEndGesture.numberOfTouchesRequired = 1;
[self.scrollView addGestureRecognizer:self.tapEndGesture];

- (void)tapGesture:(UITapGestureRecognizer *)sender {
    if(sender==self.tapStartGesture) {
        NSLog(@"tapStartGesture");
    } else if(sender==self.tapEndGesture) {
        NSLog(@"tapEndGesture");
    }
}

Solution

  • A tap gesture only has one state - "ended". You can't detect when a tap starts using a tap gesture. As you've seen, attempting to use two tap gestures doesn't accomplish what you want.

    You need to implement the UIResponder methods touchesBegan and touchesEnded.

    You may also want to see UITapGestureRecognizer - make it work on touch down, not touch up? .