ios - MKAnnotationView subview 上的 UITapGestureRecognizer 不起作用。 swift

标签 ios xcode swift mapkit mkannotationview

我有一个带有图像 subview 的 MKAnnotationView

我尝试向它添加一个 UITapGestureRecognizer 但没有响应

var tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "annotationClicked:")                    
                imageView.userInteractionEnabled = true
                imageView.addGestureRecognizer(tapGestureRecognizer)
                pinView.addSubview(imageView)
                pinView.bringSubviewToFront(imageView)

我不知道为什么

最佳答案

要对注释 View (leftCalloutAccessoryViewrightCalloutAccessoryView)的点击使用react,您必须将 View 创建为 UIControl 的后代。然后就可以实现MKMapViewDelegate协议(protocol)的calloutAccessoryControlTapped方法了。无需使用手势识别器。

这里是一段代码,它添加了一个按钮作为 pinAnnotation 的标注附件:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if annotation is PinAnnotation {
        let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin")

        pinAnnotationView.pinColor = .Purple
        pinAnnotationView.draggable = true
        pinAnnotationView.canShowCallout = true
        pinAnnotationView.animatesDrop = true

        // button as callout accessory
        let deleteButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton
        deleteButton.frame.size.width = 44
        deleteButton.frame.size.height = 44
        deleteButton.backgroundColor = UIColor.redColor()
        deleteButton.setImage(UIImage(named: "trash"), forState: .Normal)

        pinAnnotationView.leftCalloutAccessoryView = deleteButton

        return pinAnnotationView
    }

    return nil
}

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) {
    if let annotation = view.annotation as? PinAnnotation {
        self.mapView.removeAnnotation(annotation)
    }
}

关于ios - MKAnnotationView subview 上的 UITapGestureRecognizer 不起作用。 swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26719677/

相关文章:

json - 如何处理完全动态的 JSON 响应

ios - 使用核心文本时出现 EXC_BAD_ACCESS 错误

ios - 多个表和关系

ios - 试图让后台获取工作

ios - XML 到 Xcode 解析

ios - 将 dSYM 上传到 crashlytics 时出现问题

ios - 在 Swift 2 的绘图应用程序中创建一个 'undo' 函数

iphone - ARC 循环保留检测

ios - 我将如何在同一测试设备上安装两个不同版本的应用程序?

ios - 删除 UIWebView 上显示 ppt 时的填充/间隙