【问题标题】:Swift 3.0 Update Annotation on PinSwift 3.0 更新 Pin 上的注释
【发布时间】:2017-10-22 21:06:34
【问题描述】:

我的图钉上有一个注解,用于我的 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
                }
    }
}

【问题讨论】:

    标签: ios swift mkmapview mkannotation mkannotationview


    【解决方案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)
            }
        }
    }
    

    【讨论】:

    • 我也可以更改此委托中的引脚注释颜色吗?
    • 是的,你可以。 let pin = view as! MKPinAnnotationView; pin.pinTintColor = .blue
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-29
    • 2011-02-02
    • 1970-01-01
    相关资源
    最近更新 更多