【问题标题】:Swift Annotation click buttonSwift Annotation 点击​​按钮
【发布时间】:2016-08-28 09:02:19
【问题描述】:

我有一个带有自定义注释的 mapView。我还为此添加了一个按钮,但有没有办法查看按钮是在哪个注释上按下的?

代码如下:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if !(annotation is CustomPointAnnotation) {
        return nil
    }

    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView!.canShowCallout = true
        anView!.enabled = true


        anView?.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure) 

        let imageview_left = UIImageView(frame: CGRectMake(0, 0, 20, 20))
        imageview_left.image = UIImage(named: "left.png")
        anView?.leftCalloutAccessoryView = imageview_left

        let imageview_right = UIImageView(frame: CGRectMake(0, 0, 8, 13))
        imageview_right.image = UIImage(named: "right.png")
        anView!.rightCalloutAccessoryView = imageview_right

    }
    else {
        anView!.annotation = annotation
    }


    let subtitleView = UILabel()
    subtitleView.font = UIFont(name: "GillSans-Light", size: 14)
    subtitleView.numberOfLines = 1
    subtitleView.text = annotation.subtitle!
    anView!.detailCalloutAccessoryView = subtitleView


    let cpa = annotation as! CustomPointAnnotation
    anView!.image = UIImage(named:cpa.imageName)

    return anView
}

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {

    if control == view.rightCalloutAccessoryView {
        print("Pressed!")

    }
    print("Click")
}

当我点击右边的CalloutAccessoryView 时,什么也没有发生。当我单击标题或副标题注释时,也没有任何反应。我做错了什么?

【问题讨论】:

    标签: ios swift mkmapview mkannotation mkannotationview


    【解决方案1】:

    你这样做的方法是给按钮添加一个目标:

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        // ...
    
        if anView == nil {
            // ... 
            let button = MyButton(type: .DetailDisclosure)
            button.addTarget(self, action: #selector(ViewController.showAnnotationDisclosure(_:)), forControlEvents: .TouchUpInside)
    
            anView!.rightCalloutAccessoryView = button
        }
    
        return anView
    }
    
    func showAnnotationDisclosure(sender: AnyObject) {
        print("Disclosure button clicked")
    }
    

    现在如果你想知道点击了什么注释,你必须继承UIButton

    class MyButton : UIButton {
        var annotation: CustomPointAnnotation? = nil
    }
    
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        // ...
        if anView == nil {
            // ...
            let button = MyButton(type: .DetailDisclosure)
            button.addTarget(self, action: #selector(ViewController.showAnnotationDisclosure(_:)), forControlEvents: .TouchUpInside)
    
            anView!.rightCalloutAccessoryView = button
        }
    
        if let button = anView!.rightCalloutAccessoryView as? MyButton {
            button.annotation = annotation
        }
    
        return anView
    }
    
    func showAnnotationDisclosure(sender: MyButton) {
        print("Disclosure button clicked")
        print(sender.annotation?.title)
    }
    

    【讨论】:

    • 它正在工作!谢谢,但我的代码有一个错误:button.annotation = annotation 错误:无法将“MKAnnotation”类型的值分配给“CustomPointAnnotation?”当然我也不知道点击了什么注释。
    • 使用 as! 将其转换为 CustomPointAnnotation!
    • 谢谢。 button.annotation = 注释为! CustomPointAnnotation
    【解决方案2】:

    根据苹果文档,按下leftCalloutAccessoryViewrightCalloutAccessoryView 的委托方法只有在它们是UIControl 的后代时才会被调用

    所以要么这样做,要么自己处理手势,例如将UITapGestureRecognizer 的实例添加到适当的视图中。 请记住,如果这些是 UIImageView 的实例,您还必须设置 instance.userInteractionEnabled = true

    docs

    【讨论】:

      猜你喜欢
      • 2018-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-22
      • 2018-06-02
      相关资源
      最近更新 更多