Ignore mapView:didSelectAnnotationView when long press is occuring
我正在为此苦苦挣扎。我在一个小区域有很多别针的mapview。长按地图时,无论是否长按注解视图,我都想忽略mapview对注解的选择。似乎是在touchDown上选择了注释,而不是在注释视图上在内部进行了润饰,这很烦人。
我在地图视图中添加了一个长按手势:
1 2 3 | UILongPressGestureRecognizer *longRec = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(addPin:)]; longRec.cancelsTouchesInView = YES; [self.mapView addGestureRecognizer:longRec]; |
当我长按注释视图时,无法识别此内容。当我按下" did select"注释委派调用后,长按就永远不会触发。
我试图阻止点击手势识别器,但这当然是行不通的,因为mapview的手势没有委托给我的map view控制器,所以这行不通:
1 2 3 4 5 6 | - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]]) { return NO; } return YES; } |
我还尝试将长按手势添加到注解视图中作为一种hack,但是这种方法也不会被触发,而且我还是不喜欢这种策略。
当长按手势悬停在地图视图上时,是否可以阻止地图视图的注释选择?
找出解决方案。基本上是MKMapView的子类,并实现handleTap和handleLongPress。
在那儿,长按时我会阻塞水龙头。在处理同时识别两个手势的情况时,我也稍作延迟:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | @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 |
注释视图具有一个名为enabled的属性,您可以将其设置为NO,以便在呈现您要显示的信息时它不会对任何触摸事件做出反应。