我的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
}
}
}
答案 0 :(得分:1)
您不必使用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)
}
}
}