不能 select MKViewAnnotation 两次吗?

Can't select MKViewAnnotation twice?

我在地图上放置了图钉,当我点击它们时,我正在调用 didSelect。该函数仅在第一次点击该引脚时被调用,之后它不会再次调用同一个引脚,除非我 select 另一个引脚然后返回并点击它。

在我看来,pin 正在 selected,而 didSelect 只能在未 selected 的 pin 中调用,所以当我点击另一个将其取消select第一个引脚并使其再次可点击。

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

我不明白为什么上面的代码不起作用。

如何让我的注释被连续点击多次?

试试这个方法deselectAnnotation

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

希望对您有所帮助

还有一个选择,那就是在annotationView上添加一个Gesture Recognizer。 这将启用显示 callout 视图(因为立即取消选择注释将不会显示它)。

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.
}

当注释不再被选中时,不要忘记删除识别器。

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

    // Remove the gesture recognizer when the annotation is no longer selected.
}