Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 防止在覆盖点击时取消选择注释_Ios_Objective C_Mkmapview_Mapkit_Mkoverlay - Fatal编程技术网

Ios 防止在覆盖点击时取消选择注释

Ios 防止在覆盖点击时取消选择注释,ios,objective-c,mkmapview,mapkit,mkoverlay,Ios,Objective C,Mkmapview,Mapkit,Mkoverlay,在mapview上,我绘制属于特定注释的多边形覆盖。我希望在点击覆盖时选择该注释。我的第一次尝试是向mapview添加一个UITapGestureRecognitor,测试点击点是否位于多边形内,并在成功时执行[mapview selectAnnotation:myAnnotation]。问题是,在这之后,地图视图会确定没有点击任何注释,因此它会再次取消选择注释 我的问题是如何最好地防止这种情况发生,我似乎找不到一个好的解决办法。我所尝试的: 创建一个新的UIgestureRecognitiz

在mapview上,我绘制属于特定注释的多边形覆盖。我希望在点击覆盖时选择该注释。我的第一次尝试是向mapview添加一个
UITapGestureRecognitor
,测试点击点是否位于多边形内,并在成功时执行
[mapview selectAnnotation:myAnnotation]
。问题是,在这之后,地图视图会确定没有点击任何注释,因此它会再次取消选择注释

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

  • 创建一个新的
    UIgestureRecognitizer
    子类,该子类只识别重叠中的点击,然后遍历
    mapView.gestureRecognitizers
    ,并在每个子类上调用
    RequireTestureRecognitizerToFail
    。但是,mapview不会通过其属性公开任何识别器
  • 对于
    属于类别的任何其他识别器,
    应在我的自定义识别器中的
    中为
    返回
    ,点击识别器。但是,似乎还有另一个识别器没有传入
  • 在那里放置一个透明的视图,并在
    点内部执行多边形检入: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];
}

它再次中断,注释再次被取消选择。

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

我正在这样做(这是可行的,但对我来说似乎很复杂,如果不清楚,请随意询问):

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

...
_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;
    }
}
-(BOOL)GestureRecognitor:(UIGestureRecognitor*)GestureRecognitor应与GestureRecognitor:(UIGestureRecognitor*)其他GestureRecognitor同时识别{
if(_gestureRecognitors==nil)
_手势识别器=[NSMutableSet];
[\u gestureRecognizers addObject:otherGestureRecognizer];
返回YES;
}
//当我识别手势时,禁用所有内容并调用一个异步任务,在该任务中重新启用
-(void)handleOverlayLp1:(UIgestureRecognitor*)识别器
{
//做你的事。
if(recognizer.state==UIgestureRecognitizerStateStart)
{
BOOL-found=NO;
...
如果(找到){
//禁用手势,这将强制它们失败,然后在异步调用的selectOverlayAnnotation中重新启用
用于(UIgestureRecognitor*其他识别器在_gestureRecognitors中){
otherRecognizer.enabled=否;
[self-performSelector:@selector(selectOverlayAnnotation:)with object:polyline afterDelay:0.1];
}
}
}
}
-(无效)选择覆盖符号:(id)多段线
{
[_MapViewSelectAnnotation:多段线动画:否];
用于(UIgestureRecognitor*其他识别器在_gestureRecognitors中){
otherRecognizer.enabled=是;
}
}

看起来有点脏是的:)我也不确定它是否比我的代码已经做的更多,使用
requiregestrerecognizertofail
:在自定义识别器成功时防止所有其他识别器失败?另外,并不是说
应该在每个手势上同时使用GestureRecognitizer
进行识别,所以您会多次将相同的识别器添加到列表中。您是否曾经100%地使用过此功能?不幸的是,没有。我已经习惯了编辑中描述的解决方案,只是在点击覆盖时不更改区域。