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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11263796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:44:23  来源:igfitidea点击:

How to deselect a map annotation on second tap

iphoneiosxcodemkmapview

提问by Igor

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:

我没有找到如何使用 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];
    }
}

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.

提前致谢。

回答by Igor

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:

正如 luxsypher 所提到的,我添加了布尔值“is show”。所以我的函数如下所示:

- (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.

谢谢。

回答by Damien Locque

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.

也许您应该添加一个布尔值“可见”并因此采取行动。因为看起来你的手势被调用,然后“did Select”被再次调用。