My task is to deselect a map annotatnion on second tap.
I didn't find how to do it with mapView functions. So I used an article from stackoverflow and do like this:
- (void)viewDidLoad
{
annotationTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(annotationTapRecognized:)];
annotationTap.numberOfTapsRequired = 1;
annotationTap.delegate = self;
}
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
[view addGestureRecognizer:annotationTap];
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
[view removeGestureRecognizer:annotationTap];
}
- (void)annotationTapRecognized:(UIGestureRecognizer *)gesture
{
NSArray *selectedAnnotations = self.viewMap.selectedAnnotations;
for (MapAnnotation *annotationView in selectedAnnotations) {
[self.viewMap deselectAnnotation:annotationView animated:NO];
}
}
It seems works correct, but it is not. When I tap on the annotation second time callout disappears and appears again.
Any ideas?
Thanks in advance.
I've found the solution. Maybe it's not good.
I've added boolean "is show", as luxsypher mentioned. So my functions looks like the following:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
[view addGestureRecognizer:annotationTap];
if (isShow) {
NSArray *selectedAnnotations = self.viewMap.selectedAnnotations;
for (MapAnnotation *annotationView in selectedAnnotations) {
[self.viewMap deselectAnnotation:annotationView animated:YES];
}
isShow = FALSE;
}
}
- (void)annotationTapRecognized:(UIGestureRecognizer *)gesture
{
NSArray *selectedAnnotations = self.viewMap.selectedAnnotations;
for (MapAnnotation *annotationView in selectedAnnotations) {
[self.viewMap deselectAnnotation:annotationView animated:YES];
}
isShow = TRUE;
}
Maybe it will be useful for somebody :).
Thanks.
Maybe you should add a boolean "is visible" and act consequently. Cause it looks like you gesture is called and then "did Select" is called again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With