How to unmark a map annotation on a second tap

My task is to uncheck the map annotation during the second press.

I did not find how to do this using mapView functions. So I used an article from stackoverflow and did 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 to work correctly, but it is not. When I click on the annotation, the second callout disappears and appears again.

Any ideas?

Thanks in advance.

+6
source share
2 answers

I have found a solution. Maybe this is not good.

I added boolean "is show" as luxsypher said. So my functions are as follows:

 - (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 someone :).

Thanks.

+18
source

Maybe you should add a logical β€œvisible” and act accordingly. The reason is that it looks like what you call a gesture, and then β€œdid Select” is called again.

+1
source

Source: https://habr.com/ru/post/919337/


All Articles