为什么UILongPressGestureRecognizer会返回两次

时间:2014-10-02 13:38:45

标签: ios xcode swift

以下代码似乎打印了两次值,即使我按住了2秒钟。

无论我改变它的持续时间总是似乎执行两次,有人知道为什么会这样吗?

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)"]);
    })


}

2 个答案:

答案 0 :(得分:2)

检查UIGestureRecognizer的state属性,你可能会得到开始和结束。

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

答案 1 :(得分:0)

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)
    }

}