MKMapView检测左或右

时间:2018-03-25 08:38:50

标签: ios swift mkmapview accessoryview

iOS 11.x Swift 4.0

了解mapview并使用此代码创建一个带左右附件视图的图钉。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    if annotation is MKUserLocation  {
        return nil
    }

    let reuseId = "pin"
    var pav:MKPinAnnotationView?
    if (pav == nil)
    {
        pav = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pav?.isDraggable = true
        pav?.canShowCallout = true;
        pav?.rightCalloutAccessoryView = UIButton(type: .infoLight)
        pav?.leftCalloutAccessoryView = UIButton(type: .contactAdd)
    }
    else
    {
        pav?.annotation = annotation;
    }

    return pav;
}

使用此调用来检测何时按下infoLight和/或contactAdd UIButtons。但我努力想弄清楚如何判断哪一个被按下了?这个电话开火了吗?但是如何判断它是左边还是右边?

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    print("go figure annotationView \(view)")
    if (view.rightCalloutAccessoryView != nil) {
        print("right \(view.rightCalloutAccessoryView)")
    }
}

显然这是错误的,但是如何知道左侧还是右侧是否被轻敲?

2 个答案:

答案 0 :(得分:1)

您可以将控件转换为if (value instanceof TextChannel) { return { _type: "TextChannel", id: value.id, }; } 并检查其类型

UIButton

答案 1 :(得分:1)

请尝试以下代码:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if view.rightCalloutAccessoryView == control {
        //right accessory
    } else {
        // left Accessory
    }
}