Search code examples
iosswiftmkmapview

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?


Solution

  • 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