3
votes

In Interface Builder, I've added a press gesture recognizer to a MKMapView.

An event is sent after 1 second (I am using it to add a pin to the map). I have checked the "Cancel touches in view" behavior of my gesture recognizer, but my issue is that once the long press gesture is recognized, if you keep your finger on the screen and drag it on the map view, the event (for long press) will be sent continuously while dragging, as if it was actually a drag gesture recognizer, resulting in dozens of pins being added to my map...

How can I fix this ?

Thank you.

1

1 Answers

4
votes

According to the documentation:

Long-press gestures are continuous. The gesture begins (UIGestureRecognizerStateBegan) when the number of allowable fingers (numberOfTouchesRequired) have been pressed for the specified period (minimumPressDuration) and the touches do not move beyond the allowable range of movement (allowableMovement). The gesture recognizer transitions to the Change state whenever a finger moves, and it ends (UIGestureRecognizerStateEnded) when any of the fingers are lifted.

With the important point highlighted.

I believe you may not be filtering the state in your gesture recognizer's delegate method.

You will need something like this:-

- (void)longPressGestureRecognizerStateChanged:(UIGestureRecognizer *)recognizer {
    if (recognizer.state == UIGestureRecognizerStateBegan) {
       // do your stuff...
    }
}