Erik Booij
Erik Booij

Reputation: 135

Faster recognition of single tap on MKMapView (Swift)

In my iOS app I have an instance of an MKMapView and on single tap, I want to add a marker to the map. I've added a UITapGestureRecognizer and immediately noticed that it would fire even when a user double taps to zoom (or other interactions with the map).

I've implemented the UIGestureRecognizerDelegate method like this and this works, but makes the interaction very sluggish, because it obviously waits for the other gesture recognizers to fail.

    func gestureRecognizer(
        _ gestureRecognizer: UIGestureRecognizer,
        shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer
    ) -> Bool {
        guard gestureRecognizer == self.mapGestureRecognizer else {
            return false
        }

        return !(otherGestureRecognizer is UITapGestureRecognizer) && otherGestureRecognizer.state == .possible
    }

Is there any way to act quickly on a tap, without breaking the native gestures on the map view?

Upvotes: 1

Views: 202

Answers (1)

flanker
flanker

Reputation: 4210

Not sure there is a direct solution to this as the mapView can't know if your tap is part of another gesture until it's waited to check. My usual workaround for this is to use a long tap for adding markers to the map as this is a discrete event

Upvotes: 1

Related Questions