当我只点击注释视图和
this behavior it’s right时,我的calloutAccessoryControlTapped也被调用.但是如何检测用户是否在右侧的附件视图(在我的例子中是详细的公开按钮),而不仅仅是在视图中?
我添加了一个简单的检查,但它不工作.
import UIKit import MapKit extension MapVC: MKMapViewDelegate,CLLocationManagerDelegate { func mapView(mapView: MKMapView,annotationView view: MKAnnotationView,calloutAccessoryControlTapped control: UIControl) { if control == view.rightCalloutAccessoryView { ... // enter here even if I tapped on the view annotation and not on button } } }
解决方法
要实现它,您需要添加正确的配件视图的目标.您可以通过将代码片段中所示的按钮设置为rightCalloutAccessoryView来实现.
class MapViewController: UIViewController,MKMapViewDelegate { func mapView(mapView: MKMapView,viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { if annotation is Annotation { let annotationView = AnnotationView(annotation: annotation,reuseIdentifier: "reuseIdentifier") let rightButton = UIButton(type: .DetailDisclosure) rightButton.addTarget(self,action: #selector(didClickDetailDisclosure(_:)),forControlEvents: .TouchUpInside) annotationView.rightCalloutAccessoryView = rightButton } return nil } func didClickDetailDisclosure(button: UIButton) { // TODO: Perform action when was clicked on right callout accessory view. } } // Helper classes. class Annotation: NSObject,MKAnnotation { var coordinate: CLLocationCoordinate2D var title: String? var subtitle: String? init(coordinate: CLLocationCoordinate2D,title: String,subtitle: String) { self.coordinate = coordinate self.title = title self.subtitle = subtitle } } class AnnotationView: MKAnnotationView { }