意外的双重功能通话

时间:2018-07-19 20:04:24

标签: ios swift uigesturerecognizer

我有一个手势要在地图上添加一个图钉,并且当我添加该图钉时,我会进行地址解析以获取该图钉地址。 问题在于,每次我触摸地图以添加图钉时,它都会无缘无故地调用两次手势功能

每次我触摸地图时,print("city:", placemark.locality ?? "")行都会被调用两次

class PlaceYourPinpointViewController: UIViewController, UIGestureRecognizerDelegate {

    // MARK: - Variables

    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var nextBarButton: UIBarButtonItem!
    @IBOutlet weak var textView: UITextView!
    let annotation = MKPointAnnotation()

    var newLocation = CLLocation()
    let geocoder = CLGeocoder()
    var placemark: CLPlacemark?

    var addAnnotationGesture = UILongPressGestureRecognizer()
    var panGesture = UIPanGestureRecognizer()

    // MARK: - IOS Basic

    override func viewDidLoad() {
        super.viewDidLoad()
        addPinGesture()
        didDragMap()
        removePinGesture()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        setLaunchZoom()
    }

    // MARK: - Actions

    @IBAction func cancel() {
        dismiss(animated: true, completion: nil)
    }

    @objc func addPin(gestureRecognizer: UIGestureRecognizer) {
        let touchPoint = gestureRecognizer.location(in: mapView)
        let newCoordinates = mapView.convert(touchPoint, toCoordinateFrom: mapView)

        annotation.coordinate = newCoordinates
        self.mapView.addAnnotation(annotation)

        geocode(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude) { placemark, error in
            guard let placemark = placemark, error == nil else { return }
            // you should always update your UI in the main thread
                print("city:", placemark.locality ?? "")
                self.textView.text = self.string(from: placemark)
        }
        nextBarButton.isEnabled = true
    }

    @objc func removePin(gestureRecognizer: UIGestureRecognizer) {
        self.mapView.removeAnnotation(annotation)
    }

    // MARK: - Private Methods

    func addPinGesture() {
        addAnnotationGesture = UILongPressGestureRecognizer(target: self, action: #selector(addPin))
        addAnnotationGesture.minimumPressDuration = 0.06
        mapView.addGestureRecognizer(addAnnotationGesture)
    }

    func removePinGesture() {
        let removeAnnotationGesture = UITapGestureRecognizer(target: self, action: #selector(removePin))
        removeAnnotationGesture.numberOfTapsRequired = 1
        self.mapView.addGestureRecognizer(removeAnnotationGesture)
    }

    func didDragMap() {
        panGesture = UIPanGestureRecognizer(target: self, action: nil)
        panGesture.delegate = self
        mapView.addGestureRecognizer(panGesture)
    }

    func setLaunchZoom() {
        let region = MKCoordinateRegion(center: mapView.userLocation.coordinate, latitudinalMeters: 600, longitudinalMeters: 600)
        mapView.setRegion(mapView.regionThatFits(region), animated: true)
    }

    func string(from placemark: CLPlacemark) -> String {
        // make address label

        var line = ""
        line.add(text: placemark.subThoroughfare)
        line.add(text: placemark.thoroughfare, separatedBy: " ")
        line.add(text: placemark.locality, separatedBy: ", ")
        line.add(text: placemark.country, separatedBy: ", ")
        line.add(text: placemark.postalCode, separatedBy: ", ")
        return line
    }

    func geocode(latitude: CLLocationDegrees, longitude: CLLocationDegrees, completion: @escaping (CLPlacemark?, Error?) -> ())  {
        CLGeocoder().reverseGeocodeLocation(CLLocation(latitude: latitude, longitude: longitude)) { placemarks, error in
            guard let placemark = placemarks?.first, error == nil else {
                completion(nil, error)
                return
            }
            completion(placemark, nil)
        }
    }

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        // Do not begin the long press unless the pan fails.
        if gestureRecognizer == self.panGesture && otherGestureRecognizer == self.addAnnotationGesture {
            return true
        }
        return false
    }
}

1 个答案:

答案 0 :(得分:1)

该手势具有您必须在开始或结束时调用它的状态

@objc func addPin(gestureRecognizer: UIGestureRecognizer) {   
   if gestureRecognizer.state != .began {
     return 
  }
}