MKAnnotationView - 很难拖动



我有一个MKAnnotationView,用户可以在地图上拖动。

用户很难拖动图钉。 我尝试增加帧大小并使用巨大的自定义图像。 但似乎没有什么能真正改变拖动大于默认值的命中区域。

因此,我必须尝试点击/拖动大约十次才能发生任何事情。

MKAnnotationView *annView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"bluedot"] autorelease];
UIImage *image = [UIImage imageNamed:@"blue_dot.png"];
annView.image = image;
annView.draggable = YES;
annView.selected = YES;
return annView;

我在这里错过了什么?

编辑:

事实证明,问题是在拖动MKAnnotationView之前需要触摸它。 我遇到了麻烦,因为附近有很多引脚,而且我的 MKAnnotationView 非常小。

我没有意识到在拖动它之前需要触摸MKAnnotationView。

为了解决这个问题,我创建了一个计时器,定期选择该MKAnnotationView。

NSTimer *selectAnnotationTimer = [[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(selectCenterAnnotation) userInfo:nil repeats:YES] retain];  

以及它调用的方法:

- (void)selectCenterAnnotation {
    [mapView selectAnnotation:centerAnnotation animated:NO];    
}    

您可以在鼠标按下时选择注释,而不是使用计时器。这样,您就不会弄乱注释选择,并且不会为每个注释始终运行计时器。

我在开发 mac 应用程序时遇到了同样的问题,在鼠标向下选择注释后,拖动效果很好。

只是来这里说,多年后的今天,这仍然对我有帮助。在我的应用程序中,用户可以用注释向下放置一个边界区域,并(希望)自由移动它们。事实证明,这种"先选择然后移动"的行为有点噩梦,而且很容易使它变得困难和愤怒。

为了解决这个问题,我将默认的注释视图设置为选定的

func mapView(_ mapView: MKMapView,
             viewFor annotation: MKAnnotation) -> MKAnnotationView? {
  let view = MKAnnotationView(annotation: annotation, reuseIdentifier: "annotation")
  view.isDraggable = true
  view.setSelected(true, animated: false)
     
  return view
}

问题是有一个注释管理器可以将所有注释重置回取消选择,所以我在这个委托方法中解决了这个问题

func mapView(_ mapView: MKMapView,
             annotationView view: MKAnnotationView,
             didChange newState: MKAnnotationView.DragState,
             fromOldState oldState: MKAnnotationView.DragState) {
  
  if newState == .ending {
    mapView.annotations.forEach({
      mapview.view(for: $0)?.setSelected(true, animated: false)
    })
  }

对于一个愚蠢的问题,这是一个愚蠢的黑客。不过它确实有效。

最新更新