Search code examples
ioscocoa-touchmapkitgesture-recognition

UITableView passes touch events to superview when it shouldn't


In the GIKAnimatedCallout sample code, double-tap gestures, two-finger tap gestures, zoom gestures, and pinch gestures are all being passed through from the UITableView to the MKMapView underneath it. I want to stop this from happening. Touch events inside the UITableView should not be passed onto the MKMapView.

I've tried adding a UIGestureRecognizer for taps, and made it an empty method, but these touch events are still sent to the MKMapView as well as the UITableView.

Looking at the UITableView's superview hierarchy through the debugger, I see that the UITableView is a descendant of the MKMapView.

I don't really know how else to approach this problem. Any pointers are appreciated.


Solution

  • if i understand correctly, you want the MKMapView not to react at all if a gesture is made on the UITableView (in the example, the myTableView property of myMKMapView is the UITableView in question). if so, you should implement

     - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch; 

    in your subclass of MKMapView (make a subclass if you dont have one. i've called the subclass myMKMapView in this example). Start off by making your subclass of MKMapView, myMKMapView, conform to the UIGestureRecognizerDelegate protocol. in the MKMapView's .h file:

    @interface myMKMapView : MKMapView <UIGestureRecognizerDelegate>
    

    After this is done, go into myMKMapView's .m file and implement the following as such:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
        UIView *view = [self.view hitTest:[touch locationInView:self.view] withEvent:nil];
    
        if ([view isDescendantOfView:(self.myTableView)]) {
            return NO;
        }
        return YES;
    }
    

    This tells myMKMapView that if a gesture is performed on its myTableView property, myMKMapView should ignore it.