覆盖叠加时防止取消选择注释

时间:2014-01-16 13:20:41

标签: ios objective-c mkmapview mapkit mkoverlay

在我的mapview上,我绘制了属于特定注释的多边形叠加层。我希望在点击叠加层时选择该注释。我的第一次尝试是在mapview中添加UITapGestureRecognizer,测试点击点是否在多边形内,并在成功时执行[mapView selectAnnotation:myAnnotation]。问题是,在此之后,mapview会确定没有任何注释,因此它会再次取消选择注释。

我的问题是如何最好地防止这种情况发生,我似乎无法找到一个好的解决方案。我尝试过:

  • 创建一个新的UIGestureRecognizer子类,只识别叠加层内的点按,然后遍历mapView.gestureRecognizers并在每个子列表上调用requireGestureRecognizerToFail。但是,mapview不会通过其属性公开任何识别器。
  • 在我的自定义识别器中,为YES点按识别器的任何其他识别器返回shouldBeRequiredToFailByGestureRecognizer isKindOfClass pointInside:withEvent。但是,似乎还有另一个识别器没有在那里传递。
  • 在那里放置一个透明视图并在- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer*)otherGestureRecognizer { [otherGestureRecognizer requireGestureRecognizerToFail:gestureRecognizer]; // can possibly do this in custom recognizer itself instead return YES; } 中进行多边形检查,但除了点击之外还会阻止任何其他手势。

编辑:

稍微调整一下之后,我的代码几乎正常工作,我知道哪里出错了。我像以前一样有自定义识别器。在其代表中我做了:

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView
{
    // displayRegion is chosen to center annotation
    [mapView setRegion:self.displayRegion animated:YES];
}

现在点击多边形内部成功阻止了取消选择。但是,当我这样做时:

{{1}}

它再次中断,并再次取消选择注释..

1 个答案:

答案 0 :(得分:0)

似乎我们遇到了同样的问题(有点不同:我试图在手势识别器中手动选择注释)

我这样做了(它有效,但对我来说似乎很复杂,如果不清楚,请随时询问更多):

我正在处理长期压力事件:

...
_lp1 = [[UILongPressGestureRecognizer alloc] 
        initWithTarget:self action:@selector(handleOverlayLp1:)];
((UILongPressGestureRecognizer*)_lp1).minimumPressDuration = 0.05;
_lp1.delegate = self;

[_mapView addGestureRecognizer:_lp1];
...

我在全局变量中收集所有手势识别器:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

if (_gestureRecognizers==nil)
    _gestureRecognizers = [NSMutableSet set];
[_gestureRecognizers addObject:otherGestureRecognizer];
return YES;
}

// when i recognize gestures, disable everything and call an asyncrhronous task where i re-enable
- (void)handleOverlayLp1:(UIGestureRecognizer*)recognizer
{

    // Do Your thing. 
    if (recognizer.state == UIGestureRecognizerStateBegan)
    {

        BOOL found=NO;

        ...

        if (found) {
            // disable gestures, this forces them to fail, and then reenable in selectOverlayAnnotation that is called asynchronously
            for (UIGestureRecognizer *otherRecognizer in _gestureRecognizers) {
                otherRecognizer.enabled = NO;
                [self performSelector:@selector(selectOverlayAnnotation:)  withObject:polyline afterDelay:0.1];
            }
        }
    }
}

- (void)selectOverlayAnnotation: (id<MKAnnotation>) polyline
{
    [_mapView selectAnnotation:polyline animated:NO];
    for (UIGestureRecognizer *otherRecognizer in _gestureRecognizers) {
        otherRecognizer.enabled = YES;
    }
}