1
votes

I have a UIView which has two subviews, one is a UIScrollView and the other is a container view for a few other subviews. The container view is covering the scroll view completely.

Views that need to handle gestures:

  • UIScrollView - should handle the default pinch and pan gestures
  • Container view - none
  • Container view subviews - should handle tap gesture

Now in order for the tap gestures to be handled by the container view subviews I implemented pointInside:withEvent: for the container view. If it recognises the point is inside one of its subviews it returns YES. This works fine. The problem is that when I pinch or pan and my finger initially touches one of the container view subviews it doesn't work. When I pinch or pan on an empty area of the container view it works as it should.

Any suggestions how to make it work?

EDIT:

I've implemented hitTest:withEvent: for the main view and got the same behavior.

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *hitTestView;

    for (UIView *subview in [self.subviews reverseObjectEnumerator])
    {
        hitTestView = [subview hitTest:[self convertPoint:point
                                                   toView:subview]
                             withEvent:event];

        if (hitTestView && ![hitTestView isKindOfClass:[ContainerView class]])
        {
            break;
        }
    }

    return hitTestView;
}

On the bottom line the question here is how does one view only handles some gestures and passes on other gestures so an underlying view could handle them.
I've read quite a lot about the subject and tried different approaches but couldn't find a straightforward solution to what seems like a pretty common issue.

1

1 Answers

2
votes

You don't actually need to handle pinch and pan gesturese on UIScrollView manually, it's going to happen automatically.

For handling container view subviews you can use UITapGestureRecognizer. For each view you need to handle tap use:

UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFirstSubview:)];
[firstSubview addGestureRecognizer:tapRecognizer];

Handler method:

- (void)handleTapFirstSubview:(UITapGestureRecognizer *)tapRecogmizer
{
    // handle tap here
}