On my mapview I draw polygon overlays that belong to a specific annotation. I want that annotation to be selected when the overlay is tapped. My first attempt was to add a UITapGestureRecognizer
to the mapview, test whether the tapped point is inside a polygon and perform [mapView selectAnnotation:myAnnotation]
on success. The problem is that after this, the mapview decides there was a tap not on any annotations, so it deselects the annotation again.
My question is how to best prevent this from happening, I don't seem to be able to find a nice solution. What I have tried:
- Create a new
UIGestureRecognizer
subclass that recognizes just taps inside overlays, then iterate throughmapView.gestureRecognizers
and callrequireGestureRecognizerToFail
on each. However, the mapview does not expose any recognizers through its property. - Return
YES
forshouldBeRequiredToFailByGestureRecognizer
in my custom recognizer for any other recognizer thatisKindOfClass
tap recognizer. However, there still seems to be another recognizer that is not passed in there. - Place a transparent view on there and do the polygon check in
pointInside:withEvent
, but does also blocks any other gestures besides only taps.
EDIT:
After poking around a bit more, I have code that is almost working, of which I know where it goes wrong. I have a custom recognizer as before. In its delegate I do:
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer
{
[otherGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer]; // can possibly do this in custom recognizer itself instead
return YES;
}
Now taps inside polygons successfully prevent deselection. However, when I then do:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView
{
// displayRegion is chosen to center annotation
[mapView setRegion:self.displayRegion animated:YES];
}
it breaks again, and the annotation gets deselected again..