Search code examples
iosobjective-cmkmapview

objective c mkmapview in UITapGestureRecognizer how can I test if they tapped on an annotation


I allow users to tap anywhere on the screen and I also allow them to tap on annotations to display text related to the annotation.

To allow them to tap anywhere on the screen I have setup a tapgesturerecoginzer.

The problem is that when they tap on an annotation, the tapgesturerecognizer event happens before the didSelectAnnotationView, and so I perform the tapgesturerecognizer when I do not want to.

Can I somehow test, in the tapgesturerecognizer, if they have tapped on an annotation ?


Solution

  • Solution :

    In my header (.h) I added the delegate:

    @interface myVC : UIViewController <UIGestureRecognizerDelegate>
    

    In .m :

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    [self.mapView addGestureRecognizer:tap];
    tap.delegate = self;
    

    Note the delegate statement that Nirav said needed to be added

    then I added Nirav's code but changed the IF statement :

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
    {
      if (([touch.view isKindOfClass:[MKPinAnnotationView class]]) || ([touch.view isKindOfClass:[MKAnnotationView class]]))
      {
        return NO;
      }
      return YES;
    }