Search code examples
iosobjective-cmkmapview

Gesture Recognizer Not Always Triggered


I am trying to make the callout bubble of a map annotation clickable(I want the title to be clickable). There's no good way to do this from what I've seen so I've implemented a gesture recognizer on the map so that I can do a hit test to determine if the callout has been tapped. It works fine most of the time, but sometimes the gesture recognizer fails to trigger.

Here is my code

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    UITapGestureRecognizer* calloutRecognizer = [[UITapGestureRecognizer alloc]
            initWithTarget:self action:@selector(calloutTapped:)];
    calloutRecognizer.cancelsTouchesInView = false;
    [self.mapView addGestureRecognizer:calloutRecognizer];
}


- (void)calloutTapped:(UITapGestureRecognizer *)gestureRecognizer
{
    CGPoint hitPoint = [gestureRecognizer locationInView:self.mapView];
    UIView *tappedView = [self.mapView hitTest:hitPoint withEvent:nil];
    // This passthrough button ends up consuming events in the callout
    // There seems to be no way to target it explicitly so we must check the class name of the view
    if([NSStringFromClass([tappedView class]) isEqualToString: @"_MKSmallCalloutPassthroughButton"]){
        if(self.mapView.selectedAnnotations.count > 0 ){
            [self clinicTappedForClinic:[self getClinicForAnnotation :self.mapView.selectedAnnotations[0]]];
        }
    }
}

I am testing using the iPhone 7 simulator


Solution

  • Add delegate to tapGestureRecognizer:

    //add <UIGestureRecognizerDelegate> to .h 
    
    //add this where you create tapGestureRecognizer...
    tapGestureRecognizer.delegate = self;
    
    
    // check tapGestureRecognizer working or not properly 
    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
        shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }