Ios UIViewRepresentableView中的绑定中断了ObserveObject功能
我有一个UIViewRepresentableView,它为我创建了一个MKMapView。我使用两个ObservedObject来处理用户可以丢弃的管脚,并跟踪当前位置。到目前为止一切正常。但是,如果另外向我的ContentView的状态(保存映射视图的状态)插入绑定,则ObserveObject的@Published属性上的更改不会导致映射视图再次刷新。我真的不明白为什么这是 保存UIViewRepresentable MapView的我的ContentViewIos UIViewRepresentableView中的绑定中断了ObserveObject功能,ios,swift,swiftui,Ios,Swift,Swiftui,我有一个UIViewRepresentableView,它为我创建了一个MKMapView。我使用两个ObservedObject来处理用户可以丢弃的管脚,并跟踪当前位置。到目前为止一切正常。但是,如果另外向我的ContentView的状态(保存映射视图的状态)插入绑定,则ObserveObject的@Published属性上的更改不会导致映射视图再次刷新。我真的不明白为什么这是 保存UIViewRepresentable MapView的我的ContentView struct Content
struct ContentView: View {
@State var showPhotos = false
@ObservedObject var regionManager: LocationManager
@ObservedObject var annotationManager: AnnotationManager
var body: some View {
NavigationView {
ZStack(alignment: .bottomTrailing) {
MyMap(showPhotos: $showPhotos, locationManager: regionManager, annotationManager: annotationManager)
Button(action: {
self.regionManager.goToUserLocation()
}) {
LocationButton()
}
}
.edgesIgnoringSafeArea(.bottom)
.navigationBarTitle("Long-press to select location", displayMode: .inline)
}
}
}
我的实际地图视图
struct MyMap: UIViewRepresentable {
@Binding var showPhotos: Bool
@ObservedObject var locationManager: LocationManager
@ObservedObject var annotationManager: AnnotationManager
func makeCoordinator() -> MyMap.Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> MKMapView {
let map = MKMapView()
map.delegate = context.coordinator
context.coordinator.map = map
locationManager.configureUserLocation()
map.showsUserLocation = true
map.setRegion(locationManager.currentRegion, animated: true)
let longPress = UILongPressGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.didLongPress(gesture:)))
longPress.minimumPressDuration = 1.0
map.addGestureRecognizer(longPress)
return map
}
func updateUIView(_ map: MKMapView, context: Context) {
if annotationManager.currentAnnotation != nil {
map.removeAnnotations(map.annotations)
map.addAnnotation(annotationManager.currentAnnotation!)
}
map.setRegion(locationManager.currentRegion, animated: true)
}
class Coordinator: NSObject, MKMapViewDelegate, CLLocationManagerDelegate {
var parent: MyMap
var map: MKMapView?
init(_ parent: MyMap) {
self.parent = parent
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
parent.showPhotos = true
}
@objc func didLongPress(gesture: UITapGestureRecognizer) {
//...
}
}
}