Ios 防止在覆盖点击时取消选择注释
在mapview上,我绘制属于特定注释的多边形覆盖。我希望在点击覆盖时选择该注释。我的第一次尝试是向mapview添加一个Ios 防止在覆盖点击时取消选择注释,ios,objective-c,mkmapview,mapkit,mkoverlay,Ios,Objective C,Mkmapview,Mapkit,Mkoverlay,在mapview上,我绘制属于特定注释的多边形覆盖。我希望在点击覆盖时选择该注释。我的第一次尝试是向mapview添加一个UITapGestureRecognitor,测试点击点是否位于多边形内,并在成功时执行[mapview selectAnnotation:myAnnotation]。问题是,在这之后,地图视图会确定没有点击任何注释,因此它会再次取消选择注释 我的问题是如何最好地防止这种情况发生,我似乎找不到一个好的解决办法。我所尝试的: 创建一个新的UIgestureRecognitiz
UITapGestureRecognitor
,测试点击点是否位于多边形内,并在成功时执行[mapview selectAnnotation:myAnnotation]
。问题是,在这之后,地图视图会确定没有点击任何注释,因此它会再次取消选择注释
我的问题是如何最好地防止这种情况发生,我似乎找不到一个好的解决办法。我所尝试的:
- 创建一个新的
子类,该子类只识别重叠中的点击,然后遍历UIgestureRecognitizer
,并在每个子类上调用mapView.gestureRecognitizers
。但是,mapview不会通过其属性公开任何识别器RequireTestureRecognitizerToFail
- 对于
属于类别的任何其他识别器,
应在我的自定义识别器中的
中为
返回
,点击识别器。但是,似乎还有另一个识别器没有传入是
- 在那里放置一个透明的视图,并在
,但也会阻止除轻触之外的任何其他手势点内部执行多边形检入: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%地使用过此功能?不幸的是,没有。我已经习惯了编辑中描述的解决方案,只是在点击覆盖时不更改区域。