Swift 3.0 更新 Pin 上的注释

Swift 3.0 Update Annotation on Pin

我的 mapView 引脚上有注释。在我的注释中,我有一个按钮,我希望能够在用户单击该按钮时更改按钮图像(从加号到减号)。我不确定在哪里可以更新它。当用户点击图钉时,我目前已经设置了我的按钮图像。在我重新加载地图之前,图像本身不会更新,但我希望在用户单击后立即更新注释视图上的按钮。我不确定如何在弹出注释时更新按钮。

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

    if let annotation = view.annotation as? MKPointAnnotation {
                let btn = UIButton(type: .detailDisclosure)
                view.rightCalloutAccessoryView = btn
                if annotation.accessibilityValue! == "Minus" {

                    let button = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
                    button.setBackgroundImage(UIImage(named: "minus"), for: .normal)
                    button.addTarget(self, action: #selector(MapVC.minus), for: .touchUpInside)
                    view.leftCalloutAccessoryView = button
                } else {
                    let button = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
                    button.setBackgroundImage(UIImage(named: "plus"), for: .normal)
                    button.addTarget(self, action: #selector(MapVC.plus), for: .touchUpInside)
                    view.leftCalloutAccessoryView = button
                }
    }
}

您不必使用 didSelect 委托方法。当用户单击按钮时,将调用 calloutAccessoryControlTapped 委托方法。您可以在此委托方法中更改按钮图像。

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == view.leftCalloutAccessoryView {
        let button = control as! UIButton
        if annotation.accessibilityValue! == "Minus" {
            button.setBackgroundImage(UIImage(named: "minus"), for: .normal)
        } else {
            button.setBackgroundImage(UIImage(named: "plus"), for: .normal)
        }
    }
}