如何在 SwiftUI 中以编程方式显示工作表中的视图?



我目前正在SwiftUI中使用Google Maps API,并试图在以编程方式点击标记infoWindow后显示工作表。

在我的应用程序的其他部分中,我显示这样的工作表,这与我在这里尝试实现的相同,但以编程方式:

https://blog.kaltoun.cz/swiftui-presenting-modal-sheet/

现在我有一个函数,可以在点击信息窗口时打印消息,但不知道如何使用该函数使 SwiftUI 视图出现在工作表中。

-

由于我使用的是 SwiftUI,因此我实现 Google Maps API 的方式与普通 Swift 略有不同。 以下是我的GMView.swift文件的基础知识,该文件处理所有谷歌地图的内容。

import SwiftUI
import UIKit
import GoogleMaps
import GooglePlaces
import CoreLocation
import Foundation

struct GoogMapView: View {
var body: some View {
GoogMapControllerRepresentable()
}
}

class GoogMapController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
var locationManager = CLLocationManager()
var mapView: GMSMapView!
let defaultLocation = CLLocation(latitude: 42.361145, longitude: -71.057083)
var zoomLevel: Float = 15.0
let marker : GMSMarker = GMSMarker()

override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.distanceFilter = 50
locationManager.startUpdatingLocation()
locationManager.delegate = self
let camera = GMSCameraPosition.camera(withLatitude: defaultLocation.coordinate.latitude, longitude: defaultLocation.coordinate.longitude, zoom: zoomLevel)
mapView = GMSMapView.map(withFrame: view.bounds, camera: camera)
mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
mapView.isMyLocationEnabled = true
mapView.setMinZoom(14, maxZoom: 20)
mapView.settings.compassButton = true
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
mapView.settings.scrollGestures = true
mapView.settings.zoomGestures = true
mapView.settings.rotateGestures = true
mapView.settings.tiltGestures = true
mapView.isIndoorEnabled = false

marker.position = CLLocationCoordinate2D(latitude: 42.361145, longitude: -71.057083)
marker.title = "Boston"
marker.snippet = "USA"
marker.map = mapView
// Add the map to the view, hide it until we've got a location update.
view.addSubview(mapView)
//        mapView.isHidden = true
}
// Handle incoming location events.
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location: CLLocation = locations.last!
print("Location: (location)")
let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude, longitude: location.coordinate.longitude, zoom: zoomLevel)
if mapView.isHidden {
mapView.isHidden = false
mapView.camera = camera
} else {
mapView.animate(to: camera)
}
}
// Handle authorization for the location manager.
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
switch status {
case .restricted:
print("Location access was restricted.")
case .denied:
print("User denied access to location.")
// Display the map using the default location.
mapView.isHidden = false
case .notDetermined:
print("Location status not determined.")
case .authorizedAlways: fallthrough
case .authorizedWhenInUse:
print("Location status is OK.")
}
}
// Handle location manager errors.
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
locationManager.stopUpdatingLocation()
print("Error: (error)")
}
}

struct GoogMapControllerRepresentable: UIViewControllerRepresentable {
func makeUIViewController(context: UIViewControllerRepresentableContext<GMControllerRepresentable>) -> GMController {
return GMController()
}
func updateUIViewController(_ uiViewController: GMController, context: UIViewControllerRepresentableContext<GMControllerRepresentable>) {
}
}

这是我在GMView.swift的UIView控制器(GMController(中放置的函数,我试图使用它来使视图出现在工作表中:

// Function to handle when a marker's infowindow is tapped
func mapView(_ mapView: GMSMapView, didTapInfoWindowOf didTapInfoWindowOfMarker: GMSMarker) {
print("You tapped a marker's infowindow!")
return
}

这是我试图显示的视图:

struct SortBy: View {
var body: some View {
VStack(alignment: .leading) {
Text("Sort By")
.font(.title)
.fontWeight(.black)
.padding(.trailing, 6)
Rectangle()
.fill(Color.blue)
.frame(width: 200, height: 200)
}
}
}

有谁知道如何让上面的函数使 SwiftUI 视图出现在工作表中?

使用 .sheet 类似,如下所示:

https://blog.appsbymw.com/posts/how-to-present-and-dismiss-a-modal-in-swiftui-155c/

您将需要:

  1. 在您的UIViewControllerRepresentable中,添加一个State对象,假设@State var showModal = false
  2. 在父视图中,使用.sheet(isPresented: $showModal) { CONTENT_VIEW(showModal: $showModal) }
  3. 在您的UIViewControllerRepresentable中,还要添加一个showModal绑定。
  4. UIViewControllerRepresentable中,使用Coordinator设置UIViewControllerGMSMapViewDelegate
  5. 在UIViewController中,现在您可以通过owner.showModal访问绑定

希望对您有所帮助。祝你好运。

相关内容

  • 没有找到相关文章

最新更新