Mapview的selectAnnotation方法调用deselect方法也是iOS [英] selectAnnotation method of Mapview calling deselect method also iOS

查看:80
本文介绍了Mapview的selectAnnotation方法调用deselect方法也是iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经解决这个问题 6 个小时了,但我仍然在挣扎.

I have been working on this problem for 6 hours and I am still struggling.

我有一个地图视图,我正在添加这样的 MKPolygons:

I have a mapview and I am adding MKPolygons like this:

for (MKPolygon *polygon in arrPolygon){
    [mapView addOverlay:polygon];
    [mapView addAnnotation:polygon];
}

我正在查找哪个多边形叠加被点击并以编程方式选择相应的注释:

I am finding which polygon overlay was tapped and select corresponding annotation programatically:

WildcardGestureRecognizer *tapges=[[WildcardGestureRecognizer alloc] init];
tapges.touchesBeganCallback = ^(NSSet * touches, UIEvent * event) {

    UITouch *touch = [touches anyObject];
    tappedOverlay = nil;
    if([touch tapCount]==1){
    for (id<MKOverlay> overlay in mapView.overlays)
    {
        MKOverlayView *view = [mapView viewForOverlay:overlay];
        if ([overlay isKindOfClass:[MKPolygon class]] && view)
        {
            // Get view frame rect in the mapView's coordinate system
            CGRect viewFrameInMapView = [view.superview convertRect:view.frame toView:mapView];
            // Get touch point in the mapView's coordinate system
            CGPoint point = [touch locationInView:mapView];
            // Check if touch is within the view
            if (CGRectContainsPoint(viewFrameInMapView, point))
            {
                tappedOverlay = overlay;

                [mapView selectAnnotation:tappedOverlay animated:NO];
                break;
            }
        }
    }
    }
};

当我这样做时,didSelectAnnotationView 和 didDeselectAnnotationView 都会为同一个 MKAnnotationView 对象调用.我的问题是,为什么要调用 Deselect 方法?

When I do this, Both didSelectAnnotationView and didDeselectAnnotationView are called for the same MKAnnotationView object. My question is, why Deselect method is being called?

当我手动选择 Annotation 时,它不会调用 Deselect 方法,这意味着它可以正常工作.

When I manually select Annotation, It does not call Deselect method, meaning it works fine.

谢谢!!!

推荐答案

我自己有解决方案.当 Tap 发生在叠加层上但在注释边界之外时,会调用 Deselect 方法.我在 TouchesBegan 中以编程方式选择的注解将被取消选择,因为该方法是在调用 Touchesbegan 方法之后调用的.

Got solution myself. As Tap occurs on overlay but outside annotation boundary, Deselect method is called. Annotation that I select programatically in TouchesBegan, will be deselected because the method is called after Touchesbegan method is called.

这篇关于Mapview的selectAnnotation方法调用deselect方法也是iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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