加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 19:52:46 所属栏目:百科 来源:网络整理
导读:我的任务是在第二次点击时取消选择地图注释. 我没有找到如何使用mapView函数.所以我使用了stackoverflow中的一篇文章并且这样做: - (void)viewDidLoad{ annotationTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(annotationT
我的任务是在第二次点击时取消选择地图注释.

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

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

有任何想法吗?

提前致谢.

解决方法

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

我已经添加了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;
}

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

谢谢.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读