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

这一生的挚爱 提交于 2020-01-23 07:47:01

问题


I am updating my app (MyWorld) to iOS 7. One of the features of the app is that you can drag pin on the map view. It seems to be broken in iOS7.

Steps to recreate the problem:

  • Adding Annotation to the map: - works fine
  • Moving Annotation (Dragging) works fine
  • Scrolling the map: Problem

Whenever I scroll the map view annotation is moved with the map. It seems like it's not attached to the right view or layer?? If the pin is not dragged map view seems to work fine and annotation stays in defined position. I wonder if this is a mistake on my side or a known issue?

I created a dummy MapViewTest project that ilusstrates the problem on github: https://github.com/DJMobileInc/MapViewTest


回答1:


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];
    }
}



回答2:


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




回答3:


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)                
        }
    }


来源:https://stackoverflow.com/questions/18973236/ios-7-mkmapview-draggable-annotation-change-its-position-when-map-is-scrolled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!