Skip to content

Instantly share code, notes, and snippets.

@enebin
Last active November 13, 2023 03:55
Show Gist options
  • Save enebin/39c18055355ea25d88a9d3771b30a266 to your computer and use it in GitHub Desktop.
Save enebin/39c18055355ea25d88a9d3771b30a266 to your computer and use it in GitHub Desktop.
struct CustomBottomSheet<Content>: View where Content: View {
@Binding var sheetHeightOffset: CGFloat
@Binding var isSheetDragging: Bool
let content: Content
let defaultOffset: CGFloat
init(
sheetHeightOffset: Binding<CGFloat>,
isSheetDragging: Binding<Bool>,
defaultOffset: CGFloat = 50,
@ViewBuilder content: () -> Content) {
self._sheetHeightOffset = sheetHeightOffset
self._isSheetDragging = isSheetDragging
self.content = content()
self.defaultOffset = defaultOffset
self.sheetHeightOffset = defaultOffset
}
var body: some View {
GeometryReader { geometry in
VStack {
self.handle()
.padding(.vertical, 7)
.gesture(
DragGesture()
.onChanged { value in
isSheetDragging = true
sheetHeightOffset -= value.translation.height
}
.onEnded(onDragEnded)
)
self.content
Spacer()
}
.frame(
width: geometry.size.width,
height: geometry.size.height
)
.background {
RoundedRectangle(cornerRadius: 25.0, style: .continuous)
.foregroundColor(.white)
.shadow(radius: 10)
}
}
.edgesIgnoringSafeArea(.all)
}
private func onDragEnded(drag: DragGesture.Value) {
isSheetDragging = false
sheetHeightOffset = defaultOffset
}
func handle() -> some View {
Capsule()
.foregroundColor(.gray.opacity(0.7))
.frame(width: 100, height: 5)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment