5

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

4

3 回答 3

18

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];
    }
}
于 2013-10-21T02:59:45.350 回答
0

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

于 2013-09-28T09:40:05.250 回答
0

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)                
        }
    }
于 2018-12-29T06:30:21.447 回答