我有MKAnnotationView
的图像子视图。
我尝试向其添加UITapGestureRecognizer
,但没有回复
var tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "annotationClicked:")
imageView.userInteractionEnabled = true
imageView.addGestureRecognizer(tapGestureRecognizer)
pinView.addSubview(imageView)
pinView.bringSubviewToFront(imageView)
恐怕我不知道为什么
答案 0 :(得分:1)
要点击注释视图(leftCalloutAccessoryView
或rightCalloutAccessoryView
),您必须将视图创建为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)
}
}