Я показываю набор аннотаций на MKMapView, используя пользовательский MKMarkerAnnotationView с displayPriority = .required, чтобы не было кластеризации или скрытия, и UIButton как его rightCalloutAccessoryView.
Когда я касаюсь аннотации на карте, выноска отображается, как и ожидалось, но когда я касаюсь выноски или ее аксессуара, если касание перекрывается с другим маркером на карте, касание не регистрируется.
Ниже приведен удобный для Playground пример проблемы. Обратите внимание, что выноска не реагирует на нажатия, когда она перекрывается с другой аннотацией на карте.
import MapKit
import PlaygroundSupport
class MapViewController: UIViewController, MKMapViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
calloutAccessoryControlTapped control: UIControl) {
print("Callout tapped!")
}
}
class CustomAnnotationView: MKMarkerAnnotationView {
override var annotation: MKAnnotation? {
willSet {
canShowCallout = true
rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
titleVisibility = .hidden
subtitleVisibility = .hidden
displayPriority = .required
}
}
}
let mapView = MKMapView(frame: CGRect(x:0, y:0, width:800, height:800))
let controller = MapViewController()
mapView.delegate = controller
mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
let coordinate1 = CLLocationCoordinate2DMake(37.334922, -122.009033)
let annotation1 = MKPointAnnotation()
annotation1.coordinate = coordinate1
annotation1.title = "Annotation 1"
annotation1.subtitle = "Subtitle 1"
let coordinate2 = CLLocationCoordinate2DMake(37.335821492347556, -122.0071341097355)
let annotation2 = MKPointAnnotation()
annotation2.coordinate = coordinate2
annotation2.title = "Annotation 2"
annotation2.subtitle = "Subtitle 2"
mapView.addAnnotation(annotation1)
mapView.addAnnotation(annotation2)
var mapRegion = MKCoordinateRegion()
let mapRegionSpan = 0.02
mapRegion.center = coordinate1
mapRegion.span.latitudeDelta = mapRegionSpan
mapRegion.span.longitudeDelta = mapRegionSpan
mapView.setRegion(mapRegion, animated: true)
let mapViewController = MapViewController()
PlaygroundPage.current.liveView = mapView
И изображение, иллюстрирующее проблему.
Любая помощь в этом будет очень признательна. Спасибо!





Мой товарищ по команде смог решить эту проблему. Идея состоит в том, чтобы отключить взаимодействие с пользователем для всех других аннотаций на карте, когда аннотация выбрана, а затем снова включить его, когда аннотация будет отменена.
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
for nearbyAnnotation in mapView.annotations {
let annotationView = mapView.view(for: nearbyAnnotation)
if annotationView != nil {
annotationView!.isUserInteractionEnabled = false
}
}
view.isUserInteractionEnabled = true
}
func mapView(_ mapView: MKMapView, didDeselect _: MKAnnotationView) {
for nearbyAnnotation in mapView.annotations {
let annotationView = mapView.view(for: nearbyAnnotation)
if annotationView != nil {
annotationView!.isUserInteractionEnabled = true
}
}
}