Biscuit128
Biscuit128

Reputation: 5408

Why does UILongPressGestureRecognizer return twice

The following code seems to print the values twice even though I hold down for 2 seconds.

No matter what duration I change to it always seems to execute twice, does anyone know why this might be?

func action(gestureRecognizer:UIGestureRecognizer){
    var touchPoint = gestureRecognizer.locationInView(self.myMap);
    var newCo = myMap.convertPoint(touchPoint, toCoordinateFromView: self.myMap);
    var annotation = MKPointAnnotation();
    annotation.coordinate = newCo;
    var loc = CLLocation(latitude: newCo.latitude, longitude: newCo.longitude);
            CLGeocoder().reverseGeocodeLocation(loc, completionHandler: {(placemarks, error)->Void in
        let pm:CLPlacemark = placemarks[0] as CLPlacemark;

        var address = pm.locality + " ," + pm.postalCode + " ," + pm.administrativeArea + " ," + pm.country;
        annotation.title = address;
        self.myMap.addAnnotation(annotation);
        println(address);
        println("\(newCo.latitude)");
        println("\(newCo.longitude)");
        //places.append(["name:":address, "lat": "\(newCo.latitude)", "lon":"\(newCo.longitude)"]);
    })


}

Upvotes: 0

Views: 521

Answers (2)

Kahueka Huntley
Kahueka Huntley

Reputation: 1

func action(gestureRecognizer:UIGestureRecognizer) {
    print("Gesture Recognized")

    if gestureRecognizer.state == UIGestureRecognizerState.Ended {
        let touchPoint = gestureRecognizer.locationInView(self.map)
        let newCoordinate:CLLocationCoordinate2D = self.map.convertPoint(touchPoint, toCoordinateFromView: self.map)
        print(newCoordinate)
        listNewCoordinates.append(newCoordinate)
        let annotation = MKPointAnnotation()
        annotation.coordinate.longitude = newCoordinate.longitude
        annotation.coordinate.latitude = newCoordinate.latitude

        self.map.addAnnotation(annotation)
    }

}

Upvotes: 0

Tim Edwards
Tim Edwards

Reputation: 261

Check the state property of the UIGestureRecognizer, you're probably getting both begin and end.

enum UIGestureRecognizerState : Int {
    case Possible
    case Began
    case Changed
    case Ended
    case Cancelled
    case Failed
}

Upvotes: 2

Related Questions