长按发生时,忽略mapView:didSelectAnnotationView

我正在为这个挣扎。 我有一个小地区的许多针脚mapview。 无论我是否在注释视图上长时间按压,我都想忽略mapview在长按地图时select的注释。 好像这个注释是在touchDown上被选中的,而不是在注解视图里面的内容,这很烦人。

我已经为mapview添加了一个longpress手势:

UILongPressGestureRecognizer *longRec = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addPin:)]; longRec.cancelsTouchesInView = YES; [self.mapView addGestureRecognizer:longRec]; 

当我长时间按压注释视图时,这是无法识别的。 只要我按下select注释委托调用处理,长按不会触发。

我试图阻止点击手势识别器,这当然不起作用,因为mapview的手势没有委托给我的地图视图控制器,所以这是行不通的:

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) { return NO; } return YES; } 

我也尝试在注解视图中添加一个longpress手势作为黑客,但是这也永远不会被解雇,而且我也不喜欢这个策略。

有一种方法来阻止mapview的注释select,当一个longpress手势待在mapview?

想出了一个解决scheme。 基本上inheritanceMKMapView并实现handleTap和handleLongPress。

在那里,我长时间按下了一个水龙头。 我也给了一点点延迟来处理两个手势同时被识别的情况:

 @implementation KWMapView // :MKMapView - (void)handleTap:(UITapGestureRecognizer *)tapGesture { CGPoint tapPoint = [tapGesture locationInView:self]; NSUInteger numberOfTouches = [tapGesture numberOfTouches]; if (numberOfTouches == 1 && tapGesture.state == UIGestureRecognizerStateEnded) { if (!self.blockTap) { id v = [self hitTest:tapPoint withEvent:nil]; if ([v isKindOfClass:[MKAnnotationView class]]) { [self addAnnotation:[v annotation]]; [self selectAnnotation:[v annotation] animated:YES]; } else { [[NSNotificationCenter defaultCenter] postNotificationName:PNMapViewDidTapMap object:tapGesture]; } } } } - (void)handleLongPress:(UILongPressGestureRecognizer*)sender { self.blockTap = YES; if (sender.state == UIGestureRecognizerStateBegan) { [[NSNotificationCenter defaultCenter] postNotificationName:PNMapViewDropPinGesture object:sender]; } if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateFailed) { double delayInSeconds = .2; dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ self.blockTap = NO; }); } } @end 

annotion视图有一个名为enabled的属性,您可以将其设置为NO,以便在呈现要显示的信息时不会对任何触摸事件做出反应。