From 4bd613a9e44d5587ab78a38069b6508d52a8c18a Mon Sep 17 00:00:00 2001 From: Myron Wells Date: Sat, 24 Apr 2021 20:11:51 -0400 Subject: [PATCH 1/2] added ability to customize animation when opening and closing bottom sheet --- Sources/BottomSheet/BottomSheet.swift | 15 +++++++++++---- Sources/BottomSheet/ViewExtension.swift | 2 ++ iOS Example/Sources/ContentView.swift | 3 ++- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Sources/BottomSheet/BottomSheet.swift b/Sources/BottomSheet/BottomSheet.swift index d4b136b..658d74d 100644 --- a/Sources/BottomSheet/BottomSheet.swift +++ b/Sources/BottomSheet/BottomSheet.swift @@ -17,6 +17,7 @@ public struct BottomSheet: 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 @@ -24,6 +25,7 @@ public struct BottomSheet: View { private let contentBackgroundColor: Color private let topBarBackgroundColor: Color private let showTopIndicator: Bool + private let animation: Animation public init( isPresented: Binding, @@ -33,6 +35,7 @@ public struct BottomSheet: View { topBarBackgroundColor: Color = Color(.systemBackground), contentBackgroundColor: Color = Color(.systemBackground), showTopIndicator: Bool, + animation: Animation = .interactiveSpring(), @ViewBuilder content: () -> Content ) { self.topBarBackgroundColor = topBarBackgroundColor @@ -64,7 +67,7 @@ public struct BottomSheet: 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)) } } @@ -75,8 +78,11 @@ public struct BottomSheet: 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 { @@ -91,7 +97,6 @@ public struct BottomSheet: View { .gesture( DragGesture() .onChanged({ (value) in - let offsetY = value.translation.height self.draggedOffset = offsetY @@ -113,6 +118,8 @@ public struct BottomSheet: View { if offsetY > self.dragToDismissThreshold { self.isPresented = false } + + self.gestureEnded = false self.draggedOffset = 0 }) ) diff --git a/Sources/BottomSheet/ViewExtension.swift b/Sources/BottomSheet/ViewExtension.swift index 106c356..d7e4d92 100644 --- a/Sources/BottomSheet/ViewExtension.swift +++ b/Sources/BottomSheet/ViewExtension.swift @@ -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 { @@ -28,6 +29,7 @@ public extension View { topBarBackgroundColor: topBarBackgroundColor, contentBackgroundColor: contentBackgroundColor, showTopIndicator: showTopIndicator, + animation: animation, content: content) } } diff --git a/iOS Example/Sources/ContentView.swift b/iOS Example/Sources/ContentView.swift index 3ea0554..0f7ce94 100644 --- a/iOS Example/Sources/ContentView.swift +++ b/iOS Example/Sources/ContentView.swift @@ -26,7 +26,8 @@ struct ContentView: View { height: 370, topBarHeight: 16, topBarCornerRadius: 16, - showTopIndicator: false + showTopIndicator: false, + animation: .spring() ) { MapSettingView() } From 9358c7f1e5ed4674485af1e4bdc9ce2bd176dd0c Mon Sep 17 00:00:00 2001 From: Myron Wells Date: Sat, 24 Apr 2021 20:13:21 -0400 Subject: [PATCH 2/2] Add ability to customize animation --- Sources/BottomSheet/BottomSheet.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Sources/BottomSheet/BottomSheet.swift b/Sources/BottomSheet/BottomSheet.swift index 658d74d..b3febaf 100644 --- a/Sources/BottomSheet/BottomSheet.swift +++ b/Sources/BottomSheet/BottomSheet.swift @@ -41,6 +41,7 @@ public struct BottomSheet: View { self.topBarBackgroundColor = topBarBackgroundColor self.contentBackgroundColor = contentBackgroundColor self._isPresented = isPresented + self._gestureEnded = State(initialValue: true) self.height = height self.topBarHeight = topBarHeight if let topBarCornerRadius = topBarCornerRadius { @@ -48,6 +49,7 @@ public struct BottomSheet: View { } else { self.topBarCornerRadius = topBarHeight / 3 } + self.animation = animation self.showTopIndicator = showTopIndicator self.content = content() } @@ -110,6 +112,7 @@ public struct BottomSheet: View { return } } + self.gestureEnded = true self.previousDragValue = value })