【发布时间】:2020-10-18 22:37:32
【问题描述】:
从 MKMapKit didSelect 注解委托中向封装的 SwiftUI 视图推送更新的适当方法是什么?
struct CompoundView: View {
@State private var selected: MKAnnotation?
var body: some View {
ZStack {
MapView(annotations: annotations, selected: $selected)
if let selected = selected {
PopUpView(selected)
}
}
}
}
MapView 被实现为带有代表的UIViewRepresentable:
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
subject.selected = view.annotation // subject is the controller's reference to the MapView wrapping MKMapView
}
将回调传递给更新 CompoundView 状态的委托会导致 XCode 触发 Modifying state during view update, this will cause undefined behavior. 它还可以防止 MKMapView 中的注释引脚被明显选择(引脚图标在第一次不会增长被点击)。
同样,将绑定从CompoundView 传递到MapView 似乎是有问题的,因为点击时图钉图标不增长的问题仍然存在。我假设由于状态更改而重建父视图。
我尝试将委托逻辑包装在 DispatchQueue.main.async 调用中,但没有成功,此时我只是在猜测。
是否有一种首选方法可以从 MKMapView 委托调用中将信息反馈给父视图,这样我们既不在视图更新中,也不会阻止地图注释在点击时正常动画?
【问题讨论】: