如何在第二次点击时取消选择地图注释

Igo*_*gor 6 iphone xcode mkmapview ios

我的任务是在第二次点击时取消选择地图注释.

我没有找到如何使用mapView函数.所以我使用了stackoverflow中的一篇文章并且这样做:

- (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];
    }
}
Run Code Online (Sandbox Code Playgroud)

它似乎工作正确,但事实并非如此.当我第二次点击注释时,标注消失并再次出现.

有任何想法吗?

提前致谢.

Igo*_*gor 18

我找到了解决方案.也许这不好.

我已经添加了boolean"is show",正如luxsypher所提到的那样.所以我的函数如下所示:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

也许它会对某些人有用:).

谢谢.