Google 地图 iOS SDK 动画地图填充 SwiftUI

Google Maps iOS SDK animate map padding SwiftUI

我想为我的 Google 地图的填充设置动画,因为当底部 sheet 出现时,地图填充会动态调整大小。

GMSMapView.padding documentation 说可以使用基于 UIView 块的动画来完成,但我不确定这将如何与 SwiftUI 应用程序结合?

底部 sheet 工作正常,它在预期时显示并将正确的填充值传递给 MapView。这只是我无法弄清楚的动画。 (底部sheet如果好奇就用这个组件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) {
    animateCameraToNewGeoFactMarker(mapView: uiViewController.mapView)
  }