diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f5c2ab98..df9921ee6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ BottomSheet Changelog ================== +#### v3.0.2 +- Added `.customThreshold(Double)` modifier #8, #88 + #### v3.0.1 - Fix CocoaPods build #85 - Fix close button not shown in dark mode #86 diff --git a/README.md b/README.md index a43f70041..6b262306d 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,7 @@ The ViewModifiers are used to customise the look and feel of the BottomSheet. `.customBackground(...)`: Changes the background of the BottomSheet. - This works exactly like the native SwiftUI `.background(...)` modifier. +- Using offset or shadow may break the hiding transition. `.onDragChanged((DragGesture.Value) -> Void)`: Adds an action to perform when the gesture’s value changes. @@ -211,6 +212,11 @@ The ViewModifiers are used to customise the look and feel of the BottomSheet. `.enableTapToDismiss(Bool)`: Makes it possible to dismiss the BottomSheet by tapping somewhere else. +`.customThreshold(Double)`: Sets a custom threshold which determines, when to trigger swipe to dismiss or flick through. +- The threshold must be positive and higher than 10% (0.1). +- Changing the threshold does not affect whether either option is enabled. +- The default threshold is 30% (0.3). + ## BottomSheetPosition @@ -492,9 +498,11 @@ struct ArtistSongsView: View { .customBackground( Color.black .cornerRadius(30) + .shadow(color: .white, radius: 10, x: 0, y: 0) ) .foregroundColor(.white) - .shadow(color: .white, radius: 10, x: 0, y: 0) + // Adding the shadow here does not break the hiding transition, but the shadow may gets added to your other views too + // .shadow(color: .white, radius: 10, x: 0, y: 0) } } ``` diff --git a/Sources/BottomSheet/BottomSheet+ViewModifiers/BottomSheet+Threshold.swift b/Sources/BottomSheet/BottomSheet+ViewModifiers/BottomSheet+Threshold.swift new file mode 100644 index 000000000..74d730669 --- /dev/null +++ b/Sources/BottomSheet/BottomSheet+ViewModifiers/BottomSheet+Threshold.swift @@ -0,0 +1,26 @@ +// +// BottomSheet+Threshold.swift +// +// Created by Lucas Zischka. +// Copyright © 2022 Lucas Zischka. All rights reserved. +// + +import Foundation + +public extension BottomSheet { + + /// Sets a custom threshold which determines, when to trigger swipe to dismiss or flick through. + /// + /// The threshold must be positive and higher than 10% (0.1). + /// Changing the threshold does not affect whether either option is enabled. + /// The default threshold is 30% (0.3) + /// + /// - Parameters: + /// - threshold: The threshold as percentage of the screen height. + /// + /// - Returns: A BottomSheet with a custom threshold. + func customThreshold(_ threshold: Double) -> BottomSheet { + self.configuration.threshold = threshold + return self + } +} diff --git a/Sources/BottomSheet/BottomSheetView/BottomSheetView+Calculations.swift b/Sources/BottomSheet/BottomSheetView/BottomSheetView+Calculations.swift index ffba19c0b..96f8d9675 100644 --- a/Sources/BottomSheet/BottomSheetView/BottomSheetView+Calculations.swift +++ b/Sources/BottomSheet/BottomSheetView/BottomSheetView+Calculations.swift @@ -53,10 +53,10 @@ internal extension BottomSheetView { // The height of the safe area when position is bottom var bottomPositionSafeAreaHeight: CGFloat { - // Only limit height when dynamic + // Only add safe area when `dynamicBottom` and not on iPad or Mac if self.bottomSheetPosition == .dynamicBottom && !self.isIPadOrMac { #if !os(macOS) - // When dynamic return safe area as height (iPhone) + // Safe area as height (iPhone) return UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 20 #else // Should never be called diff --git a/Sources/BottomSheet/BottomSheetView/BottomSheetView+HelperViews.swift b/Sources/BottomSheet/BottomSheetView/BottomSheetView+HelperViews.swift index 022e5f23a..8821da576 100644 --- a/Sources/BottomSheet/BottomSheetView/BottomSheetView+HelperViews.swift +++ b/Sources/BottomSheet/BottomSheetView/BottomSheetView+HelperViews.swift @@ -58,12 +58,12 @@ internal extension BottomSheetView { height: self.bottomSheetPosition.isDynamic && self.translation == 0 ? nil : self.height(with: geometry), alignment: self.isIPadOrMac ? .bottom : .top ) + // Clip BottomSheet for transition to work correctly for iPad and Mac + .clipped() // BottomSheet background .background( self.bottomSheetBackground(with: geometry) ) - // Clip BottomSheet for transition to work correctly for iPad and Mac - .clipped() // On iPad and Mac the BottomSheet has a padding .padding( self.isIPadOrMac ? 10 : 0 diff --git a/Sources/BottomSheet/BottomSheetView/BottomSheetView+SwitchPosition.swift b/Sources/BottomSheet/BottomSheetView/BottomSheetView+SwitchPosition.swift index b0cc24ded..046f197b8 100644 --- a/Sources/BottomSheet/BottomSheetView/BottomSheetView+SwitchPosition.swift +++ b/Sources/BottomSheet/BottomSheetView/BottomSheetView+SwitchPosition.swift @@ -54,27 +54,27 @@ internal extension BottomSheetView { )], currentHeight: CGFloat ) { - if height <= -0.1 && height > -0.3 { + if height <= -0.1 && height > -self.configuration.threshold { // Go up one position self.onePositionSwitchUp( switchablePositions: switchablePositions, currentHeight: currentHeight ) - } else if height <= -0.3 { + } else if height <= -self.configuration.threshold { // Go up to highest position self.switchToHighestPosition( switchablePositions: switchablePositions, currentHeight: currentHeight ) - } else if height >= 0.1 && height < 0.3 { + } else if height >= 0.1 && height < self.configuration.threshold { // Go down one position self.onePositionSwitchDown( switchablePositions: switchablePositions, currentHeight: currentHeight ) - } else if height >= 0.3 && self.configuration.isSwipeToDismissEnabled { + } else if height >= self.configuration.threshold && self.configuration.isSwipeToDismissEnabled { self.closeSheet() - } else if height >= 0.3 { + } else if height >= self.configuration.threshold { // Go down to lowest position self.switchToLowestPosition( switchablePositions: switchablePositions, @@ -191,7 +191,7 @@ internal extension BottomSheetView { switchablePositions: switchablePositions, currentHeight: currentHeight ) - } else if height >= 0.3 && self.configuration.isSwipeToDismissEnabled { + } else if height >= self.configuration.threshold && self.configuration.isSwipeToDismissEnabled { self.closeSheet() } else if height >= 0.1 { // Go down one position diff --git a/Sources/BottomSheet/Models/BottomSheetConfiguration.swift b/Sources/BottomSheet/Models/BottomSheetConfiguration.swift index 14f46786e..adbcae2c1 100644 --- a/Sources/BottomSheet/Models/BottomSheetConfiguration.swift +++ b/Sources/BottomSheet/Models/BottomSheetConfiguration.swift @@ -54,4 +54,5 @@ internal class BottomSheetConfiguration: Equatable { var onDismiss: () -> Void = {} var onDragEnded: (DragGesture.Value) -> Void = { _ in } var onDragChanged: (DragGesture.Value) -> Void = { _ in } + var threshold: Double = 0.3 }