UILongPressGestureRecognizer runs twice

UILongPressGestureRecognizer is launched twice when the user long clicks on the map for more than 2-4 seconds. How can I guarantee that it will be launched only once?

func action(gestureRecognizer:UIGestureRecognizer) {

    println("long pressed on map")


override func viewDidLoad() {
    super.viewDidLoad()

    manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest

    if activePlace == -1 {

        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()



    } else {

        var uilpgr = UILongPressGestureRecognizer(target: self, action: "action:")
        uilpgr.minimumPressDuration = 2.0
        myMap.addGestureRecognizer(uilpgr)

    }        
}

func action(gestureRecognizer:UIGestureRecognizer) {

    println("long pressed on map")
    var touchPoint = gestureRecognizer.locationInView(self.myMap)
    var newCoordinate = myMap.convertPoint(touchPoint, toCoordinateFromView: self.myMap)

    var annotation = MKPointAnnotation()
    annotation.coordinate = newCoordinate
    //annotation.title = "New Place"
    myMap.addAnnotation(annotation)

    var loc = CLLocation(latitude: newCoordinate.latitude, longitude: newCoordinate.longitude)

}
+4
source share
2 answers

To start gestures, you need to check the gesture recognizer state:

func action(gestureRecognizer:UIGestureRecognizer) {
    if gestureRecognizer.state == UIGestureRecognizerState.Began {
        // ...
    }
}
+27
source

. (UIGestureRecognizerStateBegan), (numberOfTouchesRequired) (minimumPressDuration), ( ). , , (UIGestureRecognizerStateEnded), .

- :

let longGesture = UILongPressGestureRecognizer(target : self,
 action : #selector(someFunc(gestureRecognizer:)))


func someFunc(gestureRecognizer: UILongPressGestureRecognizer){
if gestureRecognizer.state == .began {
//do something
}
+1

Source: https://habr.com/ru/post/1569272/


All Articles