如何使 SwiftUi 中的按钮触发 UIKit 包装器中的函数?
我可以通过长按手势将 MKPointAnnotations 添加到我的 Mapkit MapView。
现在我想通过按 Swift UI 中的按钮来删除这些标记。
我的想法是在按下按钮时设置一个变量 true 并使用该变量作为 updateUIView 函数中函数的条件。但我收到错误消息,指出我无法在此嵌套函数中引用此变量。
这是我的 addAnnotations 包装器中的一个片段。效果很好。如何在我的mapView上实现removeAnnotation函数?
func makeUIView(context: Context) -> MKMapView {
let map = MKMapView()
map.setRegion(region, animated: false)
map.showsUserLocation = true
map.delegate = context.coordinator
locationManager.delegate = context.coordinator
let longPress = UILongPressGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.addAnnotation(gesture:)))
longPress.minimumPressDuration = 1
map.addGestureRecognizer(longPress)
return map
}
class Coordinator: NSObject, MKMapViewDelegate, CLLocationManagerDelegate {
var mapView: MapView
init(mapView: MapView) {
self.mapView = mapView
}
@objc func addAnnotation(gesture: UIGestureRecognizer) {
if gesture.state == .ended {
if let mapView = gesture.view as? MKMapView {
let point = gesture.location(in: mapView)
let locationCoordinate = mapView.convert(point, toCoordinateFrom: mapView)
let myPin = MKPointAnnotation()
myPin.title = "Latitude: \(locationCoordinate.latitude), Longitude: \(locationCoordinate.longitude)"
myPin.coordinate = locationCoordinate
mapView.addAnnotation(myPin)
}
}
}
I can add MKPointAnnotations by Long Tap Gesture to my Mapkit MapView.
Now I want to remove those markers by pressing a Button in Swift UI.
My idea was to set a variable true when the button is pressed and use this variable as condition for a function in the updateUIView function. But I get the error message that i can't refer to this variable in this nested function.
Here's a snippet from my addAnnotations wrapper. It works fine. How can I implement the removeAnnotation function on my mapView?
func makeUIView(context: Context) -> MKMapView {
let map = MKMapView()
map.setRegion(region, animated: false)
map.showsUserLocation = true
map.delegate = context.coordinator
locationManager.delegate = context.coordinator
let longPress = UILongPressGestureRecognizer(target: context.coordinator, action: #selector(Coordinator.addAnnotation(gesture:)))
longPress.minimumPressDuration = 1
map.addGestureRecognizer(longPress)
return map
}
class Coordinator: NSObject, MKMapViewDelegate, CLLocationManagerDelegate {
var mapView: MapView
init(mapView: MapView) {
self.mapView = mapView
}
@objc func addAnnotation(gesture: UIGestureRecognizer) {
if gesture.state == .ended {
if let mapView = gesture.view as? MKMapView {
let point = gesture.location(in: mapView)
let locationCoordinate = mapView.convert(point, toCoordinateFrom: mapView)
let myPin = MKPointAnnotation()
myPin.title = "Latitude: \(locationCoordinate.latitude), Longitude: \(locationCoordinate.longitude)"
myPin.coordinate = locationCoordinate
mapView.addAnnotation(myPin)
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,我不知道为什么你选择在 SwiftUI 项目中使用 MapKit 和 UIKit,我假设,除非你打算支持 iOS 13,否则你可以在 SwiftUI 中完成这一切,但无论如何,这就是你如何做到这一点:
你有你的导入当然:
然后像这样创建一个viewModel:
添加一个@StateObject属性包装器(如果您在ContentView中创建实例,请避免使用@ObservedObject)
是的,您也可以使用@EnvironmentObject
然后创建一个@ObservableObject 它将与 ContentView 共享 MapViewModel 的相同实例
在 Coordinator 类中,添加将在 UIViewRepresentable updateUIView 方法中调用的函数。
Firstly, I don't know why you chose to use MapKit with UIKit in a SwiftUI project I assume, when you can do it all in SwiftUI unless you intend to support iOS 13 but anyways here is how you do it:
You have your imports of course:
Then create a viewModel like so:
Add an @StateObject property wrapper(avoid using @ObservevedObject if you are creating the instance in your ContentView)
And yes you can also use @EnvironmentObject
Then create an @ObservableObject which will share the same instance of MapViewModel from your ContentView
In your Coordinator class, add the function which will be called in your UIViewRepresentable updateUIView method.