【问题标题】:Dragging the map after dragging my MKAnnotationView does not move the MKAnnotationView with it拖动我的 MKAnnotationView 后拖动地图不会移动 MKAnnotationView
【发布时间】:2013-09-26 20:05:31
【问题描述】:

MKPinAnnotationView 不允许您将自定义图像用作“pin”并同时启用拖动,因为一旦您开始拖动,图像就会变回默认 pin。因此我使用 MKAnnotationView 而不是 MKPinAnnotationView。

虽然使用 MKAnnotationView 而不是 MKPinAnnotationView 确实让您的自定义图像显示为您的“pin”,但它不支持您使用默认 pin 获得的拖放动画。

无论如何,我的问题是,在我将自定义 MKAnnotationView 拖到地图上的新点然后移动地图本身之后,MKAnnotationView 不再随地图移动。

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    static NSString *defaultID = @"myLocation";

    if([self.annotation isKindOfClass:[PinAnnotation class]])
    {
        //Try to get an unused annotation, similar to uitableviewcells
        MKAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:defaultID];

        //If one isn't available, create a new one
        if(!annotationView)
        {
            annotationView = [[MKAnnotationView alloc] initWithAnnotation:self.annotation reuseIdentifier:defaultID];
            annotationView.canShowCallout = YES;
            annotationView.draggable = YES;
            annotationView.enabled = YES;
        }
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 32, 32)];
        imgView.image = self.passableTag.image;
        annotationView.leftCalloutAccessoryView = imgView;
        annotationView.image = [UIImage imageNamed:[Constants tagIconImageNameForTagType:self.passableTag.type]];
        return annotationView;
    }
    return nil;
}

【问题讨论】:

  • 您允许用户在任何时候将多少个PinAnnotations 放在地图上?
  • @Craig 只有一个。

标签: ios mkmapview mkannotationview mkpinannotationview


【解决方案1】:

结束拖拽后将 annotationView.dragState 设置为 MKAnnotationViewDragStateNone 即可:

    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
        if ([view.annotation isKindOfClass:[PinAnnotation class]]) {
            if (newState == MKAnnotationViewDragStateEnding) {
                view.dragState = MKAnnotationViewDragStateNone;
            }
        }
    }

【讨论】:

  • 这正是我需要的修复方法 - 将 MKAnnotationView 的 dragState 设置为 MKAnnotationViewDragStateNone。这并不明显,因为这是一个委托方法,您会期望处理状态更改的调用代码将更新此属性。似乎是 iOS 7 中引入的 Apple 错误,因为我无法在 iOS 6 上重现。
【解决方案2】:

我遇到了同样的问题,我将“setDragState”添加到我的 MKAnnotationView 类中解决了它。

This 是一个旧的解决方案,但它对我有用 (iOS8)。

【讨论】:

    【解决方案3】:

    不要重复使用 MKAnnotationView,它会起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多