1
votes

I am trying to determine a Single Tab on a View inside of a UIScrollView. The Problem is that The UIScrollview catches all the gestures.

What I tried so far: I override the following method in my UIScrollView:

-(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
    UITouch *touch = [touches anyObject];
    if([touch tapCount]== 2) return YES;

    return NO;
}

This works fine, I can now reach the UITapGestureRecognize on my UIView, unfortunately I can only detect double-taps because the [touch tapCount] == 1 is always beeing called (dragging or zooming in the UIScrollView). But actually the UIScrollview does not need the "Single-Tap-Function"

Is there a way to decide between a drag (Scroll or zoom) and a single Tap inside this method? I cant find it..

-(BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view

Thanks in advance Fabi

3

3 Answers

6
votes

It sounds like you only want the tap recognizer to succeed if the touch doesn't scroll the scroll view. This is pretty easy because the scroll view uses a pan gesture recognizer for scrolling. On iOS 5, you can just do this:

[self.tapRecognizer requireGestureRecognizerToFail:self.scrollView.panGestureRecognizer];

If you want to support older versions of iOS, you have to do this:

for (UIGestureRecognizer *recognizer in self.scrollView.gestureRecognizers) {
    if ([recognizer isKindOfClass:[UIPanGestureRecognizer class]])
        [self.tapRecognizer requireGestureRecognizerToFail:recognizer];
0
votes

touchesShouldBegin: is a delegate method on UIView. It is not part of a UIGestureRecognizer.

You'll need to create a UITapGestureRecognizer, set the number of taps you want to trigger the recognizer and add it to your view.

Here is the documentation for UITapGestureRecognizer

0
votes

I'm assuming you meant "single tap"--your scrollView belongs to the UIView tied to your controller. Assign the tap gesture recognizer to the that UIView. It gets first picks on all gestures and then passes down the ones it isn't interested in.