【问题标题】:Prevent touch events on MKMapView being detected when a MKAnnotation is tapped防止在点击 MKAnnotation 时检测到 MKMapView 上的触摸事件
【发布时间】:2013-10-30 19:25:33
【问题描述】:

我有一个 UITapGestureRecognizer,当用户点击地图时,它会在我的 MKMap 上隐藏和显示一个工具栏 - 很简单。

但是,当用户点击 MKMapAnnotation 时,我不希望地图以正常方式响应点击(上图)。此外,当用户点击地图上的其他位置以取消选择 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];
        }
    }
}

我需要以编程方式控制注释调用的启动并检测点击事件何时击中注释以实现此目的。

任何帮助将不胜感激。

【问题讨论】:

    标签: ios iphone mkmapview mkannotation mkannotationview


    【解决方案1】:

    我认为您会发现以下链接非常有用:

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

    How do I make a MKAnnotationView touch sensitive?

    第一个链接讨论(除其他外)如何防止触摸传播到注释,以便它们有选择地响应,第二个链接讨论如何检测触摸。

    【讨论】:

      【解决方案2】:

      我认为因为 MKMapAnnotationView 位于 MKMapView 之上,它们会获取触摸事件并对其做出响应(被选中),所以我认为您不需要手动选择注释。

      然后,如果您观看 Advanced Gesture Recognizer WWDC 2010 视频,您将看到您的 MKMapView 无论如何都会收到点击事件,即使它位于注释视图下方。这可能就是您的 -(void)mapViewTapped:(UITapGestureRecognizer *)tgr 方法被调用的原因。

      除此之外,我不明白为什么您的if ([v isKindOfClass:[MKAnnotationView class]]) 永远不会是真的。我在我的代码中做同样的事情,它工作正常!

      最后,回答你的最后一个问题,如果你不想在用户试图关闭标注时做任何事情,你可以像这样跟踪自定义 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;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-14
        相关资源
        最近更新 更多