Ios UILongPressGestureRecognitor被解雇两次

Ios UILongPressGestureRecognitor被解雇两次,ios,swift,uigesturerecognizer,Ios,Swift,Uigesturerecognizer,当用户长按地图超过2-4秒时,UILongPressGestureRecognitor将被触发两次。我如何确保它只会被解雇一次 func action(gestureRecognizer:UIGestureRecognizer) { println("long pressed on map") override func viewDidLoad() { super.viewDidLoad() manager = CLLocationManager() ma

当用户长按地图超过2-4秒时,UILongPressGestureRecognitor将被触发两次。我如何确保它只会被解雇一次

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)

}

您必须检查手势识别器的
状态
以了解手势的开始:

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

长时间的新闻手势是连续的。当在指定的时间段(minimumPressDuration)内按下允许的手指数(numberOfTouchesRequired)且触摸未超出允许的移动范围(allowableMovement)时,手势开始(UIGestureRecognitizerStateStarted)。每当手指移动时,手势识别器将转换为“更改”状态,当任何手指抬起时,手势识别器将结束(UIgestureRecognitizerStateEnded)

试着这样做:

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


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