swift - Google map iOS SDK 动画 map 填充 SwiftUI

标签 swift swiftui google-maps-sdk-ios gmsmapview

我希望对 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/

相关文章:

ios - 通过确保每次使用数组的不同元素来创建 for 循环

ios - SwiftUI |如果被 ScrollView 取消,则从不调用 DragGestures onEnded

ios - Google MapView 控件没有 "swallow"手势,导致包含 scrollview 消耗它们

swiftui - 拖放以重新排序文本,无需使用列表/表单

ios - 如何从谷歌地图中的经纬度获取位置详细信息?

ios - 在 iOS 9 中使用 GMSPlace Picker 时应用程序崩溃

swift - 如何从复杂的字典中获取值?

ios - 如何从按钮集合中找到位置?

ios - iOS 14.3 上的 UICollectionViewCompositionalLayout 错误

ios - 如何在 SwiftUI 中恢复与 UIKit (interactivePopGestureRecognizer) 中相同的行为