didSelect 只为同一个 pin 触发一次

didSelect only firing once for same pin

我有一张地图,上面放置了很多图钉。我还有以下代码

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
    print("Test")
}

当我点击 pin 时,函数被正确输入并且 "Test" 被打印到控制台。我面临的问题是,在点击同一引脚后,不会执行上述代码。

要让代码再次执行,我必须点击另一个引脚,然后再次点击原来的引脚。

无论我点击 pin 多少次,如何触发上述功能

该方法在 选择 引脚时调用,而不是在点击它时调用 - 尽管第一次点击它会导致选择,该选择将一直保留到另一个引脚被点击,或者以编程方式清除选择。

您需要在使用 mapView.deselectAnnotation 再次调用该方法之前取消选择它,您可以在 didSelect.

中执行此操作

您需要再次清除 select 编辑的注释 select。

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
       let selectedAnnotations = mapView.selectedAnnotations
        for annotation in selectedAnnotations {
            mapView.deselectAnnotation(annotation, animated: false)
        }
  }