1

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

Fabi
  • 85
  • 2
  • 9

3 Answers3

6

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];
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • I have problem with this code on ios4. I catch gesture on uiscrollview with my own panrecognizer: pan.delaysTouchesBegan = YES; pan.delaysTouchesEnded = NO; This settings works fine on ios5. When my gesture recognizer delegate returns YES in shouldReceiveTouch method, scroll gesture did not call any changes with scrollViewDidScroll. – Valerii Pavlov Jul 13 '12 at 08:05
0

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

Randall
  • 14,691
  • 7
  • 40
  • 60
  • Hello Randall, thanks for your answere. No touchesShouldBegin is not a delegate of UIView, it inherits from UIScrollView http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIScrollView_Class/Reference/UIScrollView.html Of course I created the UIGestureRecognizer. But the problem is that my UIView, which owns the UITapGestureRecognizer, is inside of a UIScrollView and that the ScrollView catches all the gestures. With overriding the Method touchesShouldBegin it is possible to catch the gesture and give it to my UIView. But how can I decide between a drag and a tap? – Fabi Nov 29 '11 at 07:24
0

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.

ronalchn
  • 12,225
  • 10
  • 51
  • 61