How can drag MKPinAnnotationView pin only one tap?

5.2k views Asked by At

i add MKPinAnnotationView and setDragAble. my code is here

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
    [annotationView setDraggable:YES];
    annotationView.pinColor = MKPinAnnotationColorPurple;
    return [annotationView autorelease];
}

ok i can drag pin.

but one problem is it's not just one tap. always need second tap.

when i first tap pin is selected but can't drag. when i tap again it's available drag.

what's wrong? i want just one tap drag like "Map.app"

4

There are 4 answers

2
seapy On BEST ANSWER

Reslove this problem. ^^

i think for drag pin, pin is already selected.

so selected MKPinAnnotationView when it init.

my new code.

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    MKPinAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"pin"];
    [annotationView setDraggable:YES];
    annotationView.pinColor = MKPinAnnotationColorPurple;
    [annotationView setSelected:YES animated:YES];
    return [annotationView autorelease];
}
0
Rog On

To drag a pin you need to tap and hold, not double tap.

You will not be able to change this behaviour without using a private API which will most likely get your app rejected.

1
Armin On

The answers here doesn't account for when you tap on a pin first and deselect it after, then the dragging problem will return because mapView deselects the annotation view. I came accross a new solution and that is selecting the annotationview that just got deselected in:

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
    [view setSelected:YES];
}

This will save you from all the frustration. The beauty of it is that this is all you need! You don't need to set the pins as selected at all.

1
Rick On

Update: Nope, I'm wrong. I still sometimes require more than one tap. Here’s my erroneous answer:

I ran into this problem today. It seemed to be because I wasn't properly changing the drag state, as indicated in the docs for -setDragState:animated: (ignore the docs for -dragState, it never calls setDragState:).

In response to these changes, your custom implementation of this method should do the following:

When the drag state changes to MKAnnotationViewDragStateStarting, set the state to MKAnnotationViewDragStateDragging. If you perform an animation to indicate the beginning of a drag, and the animated parameter is YES, perform that animation before changing the state. When the state changes to either MKAnnotationViewDragStateCanceling or MKAnnotationViewDragStateEnding, set the state to MKAnnotationViewDragStateNone. If you perform an animation at the end of a drag, and the animated parameter is YES, you should perform that animation before changing the state.

Before I did this, I almost always had to tap twice, then hold. After doing this, I have yet to have to tap a second time.