MKAnnotationView子视图上的UITapGestureRecognizer无法正常工作。迅速

时间:2014-11-03 17:32:57

标签: ios xcode swift mapkit mkannotationview

我有MKAnnotationView的图像子视图。

我尝试向其添加UITapGestureRecognizer,但没有回复

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

恐怕我不知道为什么

1 个答案:

答案 0 :(得分:1)

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

这是一段代码剪辑,它将一个按钮添加为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)
    }
}