检测calloutAccessoryControlTapped只点击rightCalloutAccessoryView

Mas*_*eni 12 mapkit uicontrol ios swift

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
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

Ram*_*mis 5

为了实现它,您需要为正确的附件视图添加目标。您可以通过将按钮设置为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 {

}
Run Code Online (Sandbox Code Playgroud)