gpt4 book ai didi

swiftui - 如何在 SwiftUI 的 View 中调用方法

转载 作者:行者123 更新时间:2023-12-04 16:41:17 28 4
gpt4 key购买 nike

刚刚开始使用 SwiftUI。

我有一个 GoogleMapsViewContentView使用 CLLocationManager我在 AppDelegate 中捕获事件或 SceneDelegate通过使用 CLLocationManagerDelegate 扩展它们来上课.

如何调用 GoogleMapsView 中的方法来自 AppDelegateSceneDelegate

在这种情况下,我想调用 .animate位置更改事件发送到 AppDelegate 时的方法通过 CLLocationManagerDelegate 实例,但问题确实更笼统。

最佳答案

我自己实现了CLLocationManager和MKMapView,和 map 差不多,希望对你有帮助:

简短回答:声明一个 @Binding var foo: Any 每次 foo 发生变化时,您都可以在 GoogleMapView 中进行更改,在这种情况下,foo 是您的位置,因此您可以在每次更新 foo 时调用 animate。

长答案:

首先我创建了一个符合 UIViewRepresentable 协议(protocol)的 Mapview,就像您所做的一样,但是添加了一个@Binding 变量,这是我的“触发器”。

map View :

struct MapView: UIViewRepresentable {
@Binding var location: CLLocation // Create a @Binding variable that keeps the location where I want to place the view, every time it changes updateUIView will be called
private let zoomMeters = 400

func makeUIView(context: UIViewRepresentableContext<MapView>) -> MKMapView {
let mapView = MKMapView(frame: UIScreen.main.bounds)
return mapView
}

func updateUIView(_ mapView: MKMapView, context: Context) {
//When location changes, updateUIView is called, so here I move the map:
let region = MKCoordinateRegion(center: location.coordinate,
latitudinalMeters: CLLocationDistance(exactly: zoomMeters)!,
longitudinalMeters: CLLocationDistance(exactly: zoomMeters)!)
mapView.setRegion(mapView.regionThatFits(region), animated: true)
}
}

然后我将我的 MapView 放在我的 ContentView 中,传递一个位置参数,我将在接下来解释:

内容 View :

struct ContentView: View {

@ObservedObject var viewModel: ContentViewModel

var body: some View {
VStack {
MapView(location: self.$viewModel.location)
}
}
}

在我的 ViewModel 中,我使用委托(delegate)处理位置更改,这里是代码,在注释中有更多详细信息:

class ContentViewModel: ObservableObject {
//location is a Published value, so the view is updated every time location changes
@Published var location: CLLocation = CLLocation.init()

//LocationWorker will take care of CLLocationManager...
let locationWorker: LocationWorker = LocationWorker()

init() {
locationWorker.delegate = self
}

}

extension ContentViewModel: LocationWorkerDelegate {
func locationChanged(lastLocation: CLLocation?) {
//Location changed, I change the value of self.location, it is a @Published value so it will refresh the @Binding variable inside MapView and call MapView.updateUIView
self.location = CLLocation.init(latitude: lastLocation!.coordinate.latitude, longitude: lastLocation!.coordinate.latitude)
}
}

最后是负责 CLLocationManager() 的 LocationWorker:

class LocationWorker: NSObject, ObservableObject  {

private let locationManager = CLLocationManager()
var delegate: LocationWorkerDelegate?

let objectWillChange = PassthroughSubject<Void, Never>()

@Published var locationStatus: CLAuthorizationStatus? {
willSet {
objectWillChange.send()
}
}

@Published var lastLocation: CLLocation? {
willSet {
objectWillChange.send()
}
}

override init() {
super.init()
self.locationManager.delegate = self
//...
}
}

protocol LocationWorkerDelegate {
func locationChanged(lastLocation: CLLocation?)
}

extension LocationWorker: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
self.lastLocation = location
//When location changes: I use my delegate ->
if delegate != nil {
delegate!.locationChanged(lastLocation: lastLocation)
}
}
}

关于swiftui - 如何在 SwiftUI 的 View 中调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60356182/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com