我希望对 Google map 的填充进行动画处理,因为当底部工作表出现时, map 填充会动态调整大小。
GMSMapView.padding documentation说它可以通过基于 UIView block 的动画来完成,但我不确定这将如何与 SwiftUI 应用程序结合起来?
底部工作表工作正常,它会在预期时显示并将正确的填充值传递到 MapView。只是动画我看不懂。 (如果您好奇的话,底部工作表将使用此组件 https://github.com/LucasMucGH/BottomSheet )
任何帮助将不胜感激,谢谢。
当前相关代码:
struct MapView: UIViewRepresentable {
var bottomPadding: CGFloat
private let mapView = GMSMapView(frame: .zero)
func makeUIView(context: Context) -> GMSMapView {
return mapView
}
func updateUIView(_ mapView: GMSMapView, context: Context) {
mapView.padding = UIEdgeInsets(top: 0, left: 0, bottom: bottomPadding, right: 0)
}
}
struct ContentView: View {
@State var showBottomSheet: ShowBottomSheet = .hidden
var bottomSheetHeight: CGFloat {
if showBottomSheet == .visible {
return showBottomSheet.rawValue
} else {
return 0
}
}
var body: some View {
GeometryReader { geometry in
ZStack {
// Map
MapView(bottomPadding: geometry.size.height * bottomSheetHeight)
.edgesIgnoringSafeArea(.all)
.bottomSheet(
bottomSheetPosition: $showBottomSheet,
options: [
.allowContentDrag,
.swipeToDismiss,
.noBottomPosition,
.shadow()
],
content: {}
)
}
}
}
}
enum ShowBottomSheet: CGFloat, CaseIterable {
case hidden = 0, visible = 0.5
}
最佳答案
如果有人感兴趣,我已经弄清楚了如何做到这一点。解决方案比我预期的要简单,它实际上只是将 padding 参数包装在 UIView.animate 闭包中,然后在 updateUIViewController 方法中调用它:
private func setMapPadding(mapView: GMSMapView) {
UIView.animate(withDuration: 0.5, delay: 0.18, usingSpringWithDamping: 0.75, initialSpringVelocity: 0) {
mapView.padding = UIEdgeInsets(top: 0, left: 0, bottom: bottomPadding, right: 0)
}
}
func updateUIViewController(_ uiViewController: MapViewController, context: Context) {
animateCameraToMarker(mapView: uiViewController.mapView)
}
关于swift - Google map iOS SDK 动画 map 填充 SwiftUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70249924/