Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tapping an MKAnnotation to "select" it is REALLY slow

Tags:

ios

swift

mapkit

There's almost a 0.5 second delay between tapping and when the callout is shown for an annotation on an MKMapView.

Does anyone know why this is the case, and how I can make it instantaneously responsive when a user taps on the map?

This happens even with the user location annotation that displays "Current Location" in a callout when tapped. I want it to display that instantly when tapped, no weird delay.

EDIT: I think it's due to the setSelected function that didSelectAnnotationView calls. setSelected has an 'animated' property that might be slowing it. How do I eliminate that animation?

like image 851
ARMATAV Avatar asked Feb 25 '16 22:02

ARMATAV


2 Answers

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!

like image 135
tobidude Avatar answered Nov 07 '22 14:11

tobidude


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.

like image 25
matt Avatar answered Nov 07 '22 13:11

matt