正如已经提到的,要使注释可拖动,它必须实现setCoordinate:
方法。
此外,从 iOS 7 开始,您可能还需要实现mapView:annotationView:didChangeDragState:
方法(参见可拖动图钉在地图上没有固定位置 https://stackoverflow.com/questions/22073519/draggable-pin-does-not-have-a-fixed-position-on-map and iOS MapKit 拖动注释(MKAnnotationView)不再随地图平移 https://stackoverflow.com/questions/21567082/ios-mapkit-dragged-annotations-mkannotationview-no-longer-pan-with-map).
您可以实施setCoordinate:
方法显式地自己或只是声明一个可写的coordinate
属性(命名为exactly像这样)并综合它(并且 getter 和 setter 方法将自动为您实现)。
(请注意,如果您使用预定义的MKPointAnnotation
类,你不需要这样做,因为该类已经实现了一个可设置的coordinate
财产。)
在你的NavigationAnnotation
类,实现显式的手动解决方案以与现有的一起使用theCoordinate
财产,只是add the setCoordinate:
类实现的方法(keep现有的 getter 方法):
-(void)setCoordinate:(CLLocationCoordinate2D)newCoordinate
{
self.theCoordinate = newCoordinate;
}
您可能还需要实施didChangeDragState:
实现地图视图委托的类中的方法(与具有viewForAnnotation
方法),否则拖动后,注释视图将悬停在地图上方,即使在地图下方平移或缩放也是如此。该方法的示例实现如下:克里斯·K. 在他的回答中 https://stackoverflow.com/a/22073854/467105:
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateStarting)
{
annotationView.dragState = MKAnnotationViewDragStateDragging;
}
else if (newState == MKAnnotationViewDragStateEnding || newState == MKAnnotationViewDragStateCanceling)
{
annotationView.dragState = MKAnnotationViewDragStateNone;
}
}