ios 7 MKMapView draggable annotation change it's position when map is scrolled

偶尔善良 提交于 2019-12-05 11:17:34

This is from the MKAnnotationView Class Reference, for the MKAnnotationViewDragStateNone constant:

MKAnnotationViewDragStateNone

The view is not involved in a drag operation. The annotation view is responsible for returning itself to this state when a drag ends or is canceled.

To fix the problem, your map view delegate will need to set the annotation view's dragState back to MKAnnotationViewDragStateNone when ever the annotation ends or cancels its drag operation.

For example:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView
                                 didChangeDragState:(MKAnnotationViewDragState)newState
                                       fromOldState:(MKAnnotationViewDragState)oldState
{
    if (newState == MKAnnotationViewDragStateEnding) {
        // custom code when drag ends...

        // tell the annotation view that the drag is done
        [annotationView setDragState:MKAnnotationViewDragStateNone animated:YES];
    }

    else if (newState == MKAnnotationViewDragStateCanceling) {
        // custom code when drag canceled...

        // tell the annotation view that the drag is done
        [annotationView setDragState:MKAnnotationViewDragStateNone animated:YES];
    }
}
gmm

I had the same problem and I solved it adding "setDragState" to my MKAnnotationView class.

This is an old solution but it worked to me (iOS7): https://stackoverflow.com/a/4457772/2410800

Swift solution :

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, didChange newState: MKAnnotationViewDragState, fromOldState oldState: MKAnnotationViewDragState)
    {
        if (newState == .ending || newState == .canceling )
        {
            view.setDragState(.none, animated: true)                
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!