0

I have a view with two swipe gesture recognizers. One left and one right.

On top of this view, i have another view with two other swipe gesture recognizers.

My problem is that any swipe i do on the second view gets sent to the parent view as well.

How do i prevent this? And, how do i prevent a touch event triggered in child view B from getting sent to parent view A?

thanks!

ps. sorry if this question was asked before.. i did try a bunch of stuff before asking, but nothing worked ..

Victor C.
  • 191
  • 4
  • 16

2 Answers2

0

I just finished a work-around that may help others that come across this thread.

The basic premise is to catch when a touch happens and remove gestures if the touch happened within a set of views. It then re-adds the gestures after the gesture recognizer handles the gestures.

@interface TouchIgnorer : UIView
@property (nonatomic) NSMutableSet * ignoreOnViews;
@property (nonatomic) NSMutableSet * gesturesToIgnore;
@end
@implementation TouchIgnorer
- (id) init
{
    self = [super init];
    if (self)
    {
        _ignoreOnViews = [[NSMutableSet alloc] init];
        _gesturesToIgnore = [[NSMutableSet alloc] init];
    }
    return self;
}
- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    CGPoint relativePt;
    for (UIView * view in _ignoreOnViews)
    {
        relativePt = [view convertPoint:point toView:view];
        if (!view.isHidden && CGRectContainsPoint(view.frame, relativePt))
        {
            for (UIGestureRecognizer * gesture in _gesturesToIgnore)
            {
                [self removeGestureRecognizer:gesture];
            }
            [self performSelector:@selector(rebindGestures) withObject:self afterDelay:0];
            break;
        }
    }
    return [super pointInside:point withEvent:event];
}

- (void) rebindGestures
{
    for (UIGestureRecognizer * gesture in _gesturesToIgnore)
    {
        [self addGestureRecognizer:gesture];
    }
}
@end
DrDisc
  • 281
  • 4
  • 12
0

This can be easily accomplished with the UIGestureRecognizer's cancelsTouchesInView property. Set this to YES on the subview's recognizers, and that should prevent the gestures from propagating to the parent view.

oltman
  • 1,772
  • 1
  • 14
  • 24
  • What if i don't have any gesture or touch recognizers on the child view? I just want to be able to push buttons and scroll without triggering the parents view recognizers. – Victor C. Mar 15 '12 at 00:09
  • Interesting. I would think that you should be able to override the `touchesBegan` method in the child view to handle that, but after a few minutes of playing around with that, I haven't found a working solution. This answer is likely what you need http://stackoverflow.com/a/5234804/144755 – oltman Mar 15 '12 at 16:39