-
-
Save enebin/39c18055355ea25d88a9d3771b30a266 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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