如何为 SwiftUI ViewRepresentable 自定义 MKMapView 委托操作?

How to make custom MKMapView delegate actions for SwiftUI ViewRepresentable?

我将 MKMapView 包裹在 ViewRepresentable

public struct MapView: UIViewRepresentable {

我想创建一个像这样工作的动作回调:

MapView().onAnnotationTapped { annotation in
   
}

MapView中我定义了

@inlinable public func onAnnotationTapped(site: (AnnotationView) -> ()) -> some View {
    return self
}

但是协调器如何提供AnnotationView

public class MapViewCoordinator: NSObject, MKMapViewDelegate {
    var mapView: MapView
    
    init(_ control: MapView) {
        self.mapView = control
    }
    
    public func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

  /// How to send from here to the MapView function? 

    }
} 
           

这是一个解决方案 - 为视图引入回调 属性 并将其注入到您的 inalinable 修饰符中。准备 Xcode 12.1 / iOS 14.1

struct MapView: UIViewRepresentable {
    func makeUIView(context: Context) -> MKMapView {
        let view = MKMapView()
        view.delegate = context.coordinator
        return view
    }
    
    func updateUIView(_ uiView: MKMapView, context: Context) {
    }

    func makeCoordinator() -> MapViewCoordinator {
        MapViewCoordinator(self)
    }
    
    private var tapCallback: ((MKAnnotationView) -> ())?    // << this one !!
    
    @inlinable public func onAnnotationTapped(site: @escaping (MKAnnotationView) -> ()) -> some View {
        var newMapView = self
        newMapView.tapCallback = site            // << here !!
        return newMapView
    }
    
    public class MapViewCoordinator: NSObject, MKMapViewDelegate {
        var mapView: MapView
        
        init(_ control: MapView) {
            self.mapView = control
        }
        
        public func mapView(_ mkMap: MKMapView, didSelect view: MKAnnotationView) {
            self.mapView.tapCallback?(view)     // << call !!
        }
    }
}