如何在第二次点击时取消选择地图注释 [英] How to deselect a map annotation on second tap

查看:100
本文介绍了如何在第二次点击时取消选择地图注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My task is to deselect a map annotatnion on second tap.

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

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.

有任何想法吗?

提前致谢。

推荐答案

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

I've found the solution. Maybe it's not good.

我已经添加了booleanis show,正如luxsypher所提到的那样。所以我的函数如下所示:

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;
}

可能对某人有用:)。

谢谢。

这篇关于如何在第二次点击时取消选择地图注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆