Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to customize Bottom Sheet opening and closing animation #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
18 changes: 14 additions & 4 deletions Sources/BottomSheet/BottomSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ 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>,
Expand All @@ -33,18 +35,21 @@ 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.height = height
self.topBarHeight = topBarHeight
if let topBarCornerRadius = topBarCornerRadius {
self.topBarCornerRadius = topBarCornerRadius
} else {
self.topBarCornerRadius = topBarHeight / 3
}
self.animation = animation
self.showTopIndicator = showTopIndicator
self.content = content()
}
Expand All @@ -64,7 +69,7 @@ public struct BottomSheet<Content: View>: View {
.frame(height: self.height - 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 - self.height/2 + geometry.safeAreaInsets.bottom + self.draggedOffset) : (geometry.size.height/2 + self.height/2 + geometry.safeAreaInsets.bottom))
}
}
Expand All @@ -75,8 +80,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.gestureEnded = false
self.isPresented = false
}
}

fileprivate func topBar(geometry: GeometryProxy) -> some View {
Expand All @@ -91,7 +99,6 @@ public struct BottomSheet<Content: View>: View {
.gesture(
DragGesture()
.onChanged({ (value) in

let offsetY = value.translation.height
self.draggedOffset = offsetY

Expand All @@ -105,6 +112,7 @@ public struct BottomSheet<Content: View>: View {
return
}
}
self.gestureEnded = true
self.previousDragValue = value

})
Expand All @@ -113,6 +121,8 @@ public struct BottomSheet<Content: View>: View {
if offsetY > self.dragToDismissThreshold {
self.isPresented = false
}

self.gestureEnded = false
self.draggedOffset = 0
})
)
Expand Down
2 changes: 2 additions & 0 deletions Sources/BottomSheet/ViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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 @@ -28,6 +29,7 @@ public extension View {
topBarBackgroundColor: topBarBackgroundColor,
contentBackgroundColor: contentBackgroundColor,
showTopIndicator: showTopIndicator,
animation: animation,
content: content)
}
}
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