ios - MKOverlayView 和触摸

标签 ios mkmapview

我的 map 上有一个自定义 MKOverlayView,我想检测触摸。但是,我似乎无法让叠加层做出响应。我希望它会像忘记将 userInteractionEnabled 设置为 YES 一样愚蠢......但是,唉,运气不好

....目前,我是这样的:

//map delegate overlay:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{

     if (_radiusView !=nil) {
          [_radiusView removeFromSuperview];
          [_radiusView release];
          _radiusView = nil;
     }
     _radiusView = [[CustomRadiusView alloc]initWithCircle:overlay];
     _radiusView.userInteractionEnabled = YES;
     _radiusView.strokeColor = [UIColor blueColor];
     _radiusView.fillColor = [UIColor grayColor];
     _radiusView.lineWidth = 1.0;
     _radiusView.alpha = 0;

     //fade in radius view
     [UIView beginAnimations:@"fadeInRadius" context:nil];
     [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
     [UIView setAnimationDuration:0.6];
     _radiusView.alpha = .3;
     [UIView commitAnimations];

     return _radiusView;

}   

我的自定义覆盖类简单地实现了 touchesBegan,并扩展了 MKCircleView

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  NSLog(@"touch!");
}

最佳答案

首先,向您的 MKMapView 添加一个手势识别器(注意:这是假设 ARC):

[myMapView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mapTapped:)]];

在识别器操作中,您可以通过如下方式确定点击点是否在 View 中:

- (void)mapTapped:(UITapGestureRecognizer *)recognizer
{
  MKMapView *mapView = (MKMapView *)recognizer.view;
  id<MKOverlay> tappedOverlay = nil;
  for (id<MKOverlay> overlay in mapView.overlays)
  {
    MKOverlayView *view = [mapView viewForOverlay:overlay];
    if (view)
    {
      // Get view frame rect in the mapView's coordinate system
      CGRect viewFrameInMapView = [view.superview convertRect:view.frame toView:mapView];
      // Get touch point in the mapView's coordinate system
      CGPoint point = [recognizer locationInView:mapView];
      // Check if the touch is within the view bounds
      if (CGRectContainsPoint(viewFrameInMapView, point))
      {
        tappedOverlay = overlay;
        break;
      }
    }
  }
  NSLog(@"Tapped view: %@", [mapView viewForOverlay:tappedOverlay]);
}

关于ios - MKOverlayView 和触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3472461/

相关文章:

ios - UIVIew animateWithDuration 线性?

ios - UIImageView 加载较大图像后调整大小

ios - 显示横幅广告时,多点触控无法在具有robovm和libGDX的iOS上运行

ios - HKHealthStore 类型的值没有类型 dateOfBirthWithError

ios - Epub 字体修改不起作用

ios - 如何根据时间在 MKMapView 上显示/隐藏夜间模式覆盖

iOS mapview 使用 word 搜索位置

ios - 如何在我的 MKMapView 中获得更细粒度的跨度

ios - 仅旋转 View 而不旋转其 subview

iphone - 使用 CLLocationManager 的正确方法(开始/停止更新用户位置)