Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't select MKViewAnnotation twice?

I've got pins placed on a map and when I tap on them I'm calling the didSelect. The function only gets called the first time the pin is tapped, and after that it's not called on that same pin again unless I select another pin and then come back and tap it.

What that sounds like to me is the pin is being selected, and didSelect can only be called in unselected pins, so when I go tap on another pin it's deselecting the first pin and making it tappable again.

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    view.isSelected = false
}

I don't understand why the above code does not work.

How can I allow my annotations be tapped more than one time in a row?

like image 897
J.Doe Avatar asked Aug 06 '17 06:08

J.Doe


2 Answers

Try with this method deselectAnnotation

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
     //do what you need here
     mapView.deselectAnnotation(view.annotation, animated: true)
}

Hope this helps

like image 63
Reinier Melian Avatar answered Nov 06 '22 21:11

Reinier Melian


There is another option, and that is to add a Gesture Recognizer for on the annotationView. This will enable showing the callout view (since de-selecting the annotation immediately will not show it).

internal func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(mapPinTapGestureRecognizer)))
}

@objc private func mapPinTapGestureRecognizer(gestureRecognizer: UITapGestureRecognizer) {

    // Will get called on the second time the pin is selected.
    // And then, after that, it will be called every time.
}

Just don't forget to remove the recognizer when the annotation is no longer selected.

internal func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {

    // Remove the gesture recognizer when the annotation is no longer selected.
}
like image 39
Raz Avatar answered Nov 06 '22 22:11

Raz