Tapping an MKAnnotation to "select" it is REALLY slow

after doing a lot of research I found a solution for this! It's a tiny tiny bit hacky, but it works like a charm.

The secret is, that when turning off zoom for the map, the didSelect listener triggers immediately. As we need zoom (of course), what we need to do is, to temporarily disable the zoom, just for the moment of the didSelect event!

In Swift:

let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
gestureRecognizer.numberOfTapsRequired = 1
gestureRecognizer.numberOfTouchesRequired = 1
gestureRecognizer.delegate = self
mapView.addGestureRecognizer(gestureRecognizer)

and

@objc func handleTap(_ sender: UITapGestureRecognizer? = nil) {
    // disabling zoom, so the didSelect triggers immediately
    mapView.isZoomEnabled = false
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
        self.mapView.isZoomEnabled = true
    }
}

This gesture event triggers before the didSelect event. So the moment the didSelect events is called, zoom is turned off and it does trigger without delay!

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    mapView.isZoomEnabled = true // Not really necessary
    // Triggered immediately, do something
}

Note: This disables doubleTap gestures for the map, but I guess they are not used too much. So if you want a quick response, you need to live with it!


My solution is to enable zooming on map and add individual tap handler on MKAnnotationView subclass.

Native zoom MKOneHandedZoomGestureRecognizer, MKZoomingPanGestureRecognizer and MKConditionalPanZoomGestureRecognizer will work.

But also immediate reaction on tap will be handled by button or tap recognizer on annotation view.


Unfortunately, there's nothing you can do about this. It's for the exact same reason that tapping links in Mobile Safari is slow: the gesture recognizers have to jostle for a while to decide whether you might be scrolling (dragging) before they agree that you are tapping.

So, it has nothing to do with the animation. It's just the nature of gesture recognition in this situation.

Tags:

Ios

Swift

Mapkit