当轻击MKAnnotation时,防止MKMapView上的触摸事件被检测到

我有一个UITapGestureRecognizer,当用户点击Map时,它会隐藏和显示我的MKMap上的一个工具栏。

但是,当用户点击MKMapAnnotation时,我不希望地图以正常方式(上图)响应水龙头。 此外,当用户点击地图上的其他地方取消select一个MKAnnotation标注,我也不希望工具栏响应。 所以,只有在当前没有选定状态的MKAnnotations时,工具栏才会响应。 当用户直接点击注释时也不应该回应。

到目前为止,我正在尝试对地图上的轻拍手势做出反应的以下操作 – 但注释视图永远不会被检测到(第一个if语句),而且无论此方法如何,注释视图也会启动。

-(void)mapViewTapped:(UITapGestureRecognizer *)tgr { CGPoint p = [tgr locationInView:self.mapView]; UIView *v = [self.mapView hitTest:p withEvent:nil]; id<MKAnnotation> ann = nil; if ([v isKindOfClass:[MKAnnotationView class]])<---- THIS CONDITION IS NEVER MET BUT ANNOTATIONS ARE SELECTED ANYWAY { //annotation view was tapped, select it… ann = ((AircraftAnnotationView *)v).annotation; [self.mapView selectAnnotation:ann animated:YES]; } else { //annotation view was not tapped, deselect if some ann is selected... if (self.mapView.selectedAnnotations.count != 0) { ann = [self.mapView.selectedAnnotations objectAtIndex:0]; [self.mapView deselectAnnotation:ann animated:YES]; } // If no annotation view is selected currently then assume control of // the navigation bar. else{ [self showToolBar:self.navigationController.toolbar.hidden]; } } } 

我需要以编程方式控制注释调用的启动,并检测触摸事件何时已经注释以实现此目的。

任何帮助,将不胜感激。

我想你会发现以下链接非常有用:

http://blog.asynchrony.com/2010/09/building-custom-map-annotation-callouts-part-2/

如何使MKAnnotationView触摸敏感?

第一个链接讨论(除别的以外)如何防止触摸传播的注释,使他们有select地响应,第二个如何检测触摸。

我认为,因为MKMapAnnotationView是在MKMapView的顶部,他们将得到触摸事件和响应(被选中),所以我不认为你需要手动select你的注释。

然后,如果您查看了“高级手势识别器”WWDC 2010video,则无论如何,即使它位于注释视图下方,您也会看到MKMapView将收到轻击事件。 这可能是为什么你的-(void)mapViewTapped:(UITapGestureRecognizer *)tgr方法被调用。

除此之外,我不明白为什么你的if ([v isKindOfClass:[MKAnnotationView class]])从不是真的。 我在我的代码中完全一样的东西,它工作正常!

最后,要回答你最后的问题,如果你不想在用户试图closures标注时做任何事情,你可以跟踪一个自定义的isCalloutOpen布尔值,像这样:

 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { //some code _isCalloutOpen = YES; } - (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view { // delay the reset because didDeselectAnnotationView could (and is often) called before your gesture recgnizer handler method get called. [self performSelector:@selector(resetCalloutOpenState) withObject:Nil afterDelay:0.1]; } - (void)resetCalloutOpenState { _isCalloutOpen = NO; } - (void)mapViewTapped:(UITapGestureRecognizer *)tgr { if (_isCalloutOpen) { return; } }