Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions Sources/BottomSheet/BottomSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ public struct BottomSheet<Content: View>: View {
@State private var previousDragValue: DragGesture.Value?

@Binding var isPresented: Bool
@State var gestureEnded: Bool
private let height: CGFloat
private let topBarHeight: CGFloat
private let topBarCornerRadius: CGFloat
private let content: Content
private let contentBackgroundColor: Color
private let topBarBackgroundColor: Color
private let showTopIndicator: Bool

private let animation: Animation

public init(
isPresented: Binding<Bool>,
height: CGFloat,
Expand All @@ -34,11 +36,14 @@ public struct BottomSheet<Content: View>: View {
topBarBackgroundColor: Color = Color(.systemBackground),
contentBackgroundColor: Color = Color(.systemBackground),
showTopIndicator: Bool,
animation: Animation = .interactiveSpring(),
@ViewBuilder content: () -> Content
) {
self.topBarBackgroundColor = topBarBackgroundColor
self.contentBackgroundColor = contentBackgroundColor
self._isPresented = isPresented
self._gestureEnded = State(initialValue: true)
self.animation = animation
self.height = height
self.topBarHeight = topBarHeight
if let topBarCornerRadius = topBarCornerRadius {
Expand All @@ -65,7 +70,7 @@ public struct BottomSheet<Content: View>: View {
.frame(height: sheetHeight(in: geometry) - min(self.draggedOffset*2, 0))
.background(self.contentBackgroundColor)
.cornerRadius(self.topBarCornerRadius, corners: [.topLeft, .topRight])
.animation(.interactiveSpring())
.animation(self.gestureEnded ? .interactiveSpring() : self.animation)
.offset(y: self.isPresented ? (geometry.size.height/2 - sheetHeight(in: geometry)/2 + geometry.safeAreaInsets.bottom + self.draggedOffset) : (geometry.size.height/2 + sheetHeight(in: geometry)/2 + geometry.safeAreaInsets.bottom))
}
}
Expand All @@ -80,8 +85,11 @@ public struct BottomSheet<Content: View>: View {
.black
.opacity(grayBackgroundOpacity)
.edgesIgnoringSafeArea(.all)
.animation(.interactiveSpring())
.onTapGesture { self.isPresented = false }
.animation(self.gestureEnded ? .interactiveSpring() : self.animation)
.onTapGesture {
self.isPresented = false
self.gestureEnded = false
}
}

fileprivate func topBar(geometry: GeometryProxy) -> some View {
Expand Down Expand Up @@ -109,9 +117,11 @@ public struct BottomSheet<Content: View>: View {
let velocityY = heightDiff / timeDiff
if velocityY > 1400 {
self.isPresented = false
self.gestureEnded = false
return
}
}
self.gestureEnded = true
self.previousDragValue = value

})
Expand All @@ -120,6 +130,7 @@ public struct BottomSheet<Content: View>: View {
if offsetY > self.dragToDismissThreshold {
self.isPresented = false
}
self.gestureEnded = false
self.draggedOffset = 0
})
)
Expand Down
6 changes: 5 additions & 1 deletion Sources/BottomSheet/ViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public extension View {
contentBackgroundColor: Color = Color(.systemBackground),
topBarBackgroundColor: Color = Color(.systemBackground),
showTopIndicator: Bool = true,
animation: Animation = .interactiveSpring(),
@ViewBuilder content: @escaping () -> Content
) -> some View {
ZStack {
Expand All @@ -29,6 +30,7 @@ public extension View {
topBarBackgroundColor: topBarBackgroundColor,
contentBackgroundColor: contentBackgroundColor,
showTopIndicator: showTopIndicator,
animation: animation,
content: content)
}
}
Expand All @@ -41,6 +43,7 @@ public extension View {
contentBackgroundColor: Color = Color(.systemBackground),
topBarBackgroundColor: Color = Color(.systemBackground),
showTopIndicator: Bool = true,
animation: Animation = .interactiveSpring(),
@ViewBuilder content: @escaping (Item) -> Content
) -> some View {
let isPresented = Binding {
Expand All @@ -58,7 +61,8 @@ public extension View {
topBarCornerRadius: topBarCornerRadius,
contentBackgroundColor: contentBackgroundColor,
topBarBackgroundColor: topBarBackgroundColor,
showTopIndicator: showTopIndicator
showTopIndicator: showTopIndicator,
animation: animation
) {
if let unwrapedItem = item.wrappedValue {
content(unwrapedItem)
Expand Down
3 changes: 2 additions & 1 deletion iOS Example/Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ struct ContentView: View {
height: 370,
topBarHeight: 16,
topBarCornerRadius: 16,
showTopIndicator: false
showTopIndicator: false,
animation: .spring()
) {
MapSettingView()
}
Expand Down