[已找到解决方法,请参阅第一个评论]
这是iOS 11:
在我的mapView上,我有一些注释
class POIView: MKMarkerAnnotationView {
static let REUSEIDENTIFIER = "POIView"
override var annotation: MKAnnotation? {
willSet {
guard let poi = newValue as? POI else { return }
canShowCallout = true
calloutOffset = CGPoint(x: -5, y: 5)
let rightCalloutButton = UIButton(type: .custom)
let rightCalloutImage = UIImage(named: "loc-map-route-line-tab")
rightCalloutButton.setImage(rightCalloutImage, for: .normal)
rightCalloutButton.frame = CGRect(x: 0, y: 0, width: rightCalloutImage?.size.width ?? 0, height: rightCalloutImage?.size.height ?? 0)
rightCalloutAccessoryView = rightCalloutButton
// some more code
clusteringIdentifier = String(describing: POIView.self)
}
}
}
这很好用,注释和标注显示在地图上。 当我点击rightCalloutAccessoryView按钮时,将调用正确的委托方法:
public func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
...
}
我也有一个集群视图:
internal final class POIClusterView: MKAnnotationView {
static let REUSEIDENTIFIER = "POIClusterView"
override var annotation: MKAnnotation? {
willSet {
if let cluster = newValue as? MKClusterAnnotation {
canShowCallout = true
let rightCalloutButton = UIButton(type: .custom)
let rightCalloutImage = UIImage(named: "loc-map-route-line-tab")
rightCalloutButton.setImage(rightCalloutImage, for: .normal)
rightCalloutButton.frame = CGRect(x: 0, y: 0, width: rightCalloutImage?.size.width ?? 0, height: rightCalloutImage?.size.height ?? 0)
rightCalloutAccessoryView = rightCalloutButton
...
}
}
}
}
}
它工作正常,显示标注,标注在右侧显示一个按钮。 但是当我点击群集标注的右键时,上述方法
public func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
...
}
没有被调用。
我该怎么做才能检测到群集标注右键上的点击?