Riccardo Queri
Riccardo Queri

Reputation: 1025

Long press drops 2 pins with each tap

I got my app dropping pin by long tap, i allow users to drop only two pins and its working i guess.. but every time i tap to add a pin in the simulator it adds two pins (not only one).. here is the code:

-(void) handleLongPressGesture:(UIGestureRecognizer*)sender
{
    if (pinId < 3) {
        // Here we get the CGPoint for the touch and convert it to
        // latitude and longitude coordinates to display on the map

        CGPoint point = [sender locationInView:self.mapView];       
        CLLocationCoordinate2D coord = [self.mapView convertPoint:point
                                            toCoordinateFromView:self.mapView];

        if (pinId == 1) {
            lat1 = coord.latitude;
            long1 = coord.longitude;

            MapAppAnnotation* annotation;
            annotation = [[MapAppAnnotation alloc] initWithCoordinate:coord
                                                andID:pinId];

            [mapView addAnnotation:annotation];

            MKCircle* circle = [MKCircle circleWithCenterCoordinate:coord
                                        radius:5000];
            [mapView addOverlay:circle];    
            pinId++;

        } else {
            lat2 = coord.latitude;
            long2 = coord.longitude;
            MapAppAnnotation* annotation2;
            annotation2 = [[MapAppAnnotation alloc] initWithCoordinate:coord
                                                andID:pinId];

            [mapView addAnnotation:annotation2];
        }
    } 
}

I would like to know if is my fault (code error..) or is the iPhone simulator that get my long-mouse-pressure like two different long pressures.. is this possible?

Upvotes: 2

Views: 1298

Answers (1)

Lachlan Roche
Lachlan Roche

Reputation: 25946

Your selector is being called once when the gesture begins, and again when it ends. Check the gesture's state and act on the relevant one.

-(void)handleLongPressGesture:(UIGestureRecognizer*)sender
{
    if (sender.state != UIGestureRecognizerStateEnded) return;

    // otherwise, handle the gesture as before
}

The class reference for UILongPressGestureRecognizer says:

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.

Upvotes: 4

Related Questions