From 63457a47a7b1e15381e8638c5871f5ec98d06546 Mon Sep 17 00:00:00 2001 From: Kevin Barnes Date: Sat, 27 May 2023 17:27:48 -0400 Subject: [PATCH 1/3] Add option to close swipe actions on label tap --- Sources/SwipeActions.swift | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Sources/SwipeActions.swift b/Sources/SwipeActions.swift index 60eb846..2bd578d 100644 --- a/Sources/SwipeActions.swift +++ b/Sources/SwipeActions.swift @@ -176,6 +176,9 @@ public struct SwipeOptions { /// Values for controlling the trigger animation. var offsetTriggerAnimationStiffness = Double(160), offsetTriggerAnimationDamping = Double(70) + + /// If true, the leading and trailing actions will close when the swipe view label is tapped. + var closeOnLabelTap = false } // MARK: - Environment @@ -408,6 +411,16 @@ public struct SwipeView: View where Labe HStack { label() .offset(x: offset) /// Apply the offset here. + .if(options.closeOnLabelTap && (trailingState == .expanded || leadingState == .expanded)) { view in + view.onTapGesture { + if trailingState == .expanded { + trailingState = .closed + } else { + leadingState = .closed + } + close(velocity: 0) + } + } } .readSize { size = $0 } /// Read the size of the parent label. .background( /// Leading swipe actions. @@ -1256,6 +1269,13 @@ public extension SwipeView { view.options.offsetTriggerAnimationDamping = damping return view } + + /// If true, the leading and trailing actions will close when the swipe view label is tapped. + func closeOnLabelTap(_ value: Bool) -> SwipeView { + var view = self + view.options.closeOnLabelTap = value + return view + } } /// Modifier for a clipped delete transition effect. @@ -1424,3 +1444,19 @@ struct AllowSwipeToTriggerKey: PreferenceKey { static var defaultValue: Bool? = nil static func reduce(value: inout Bool?, nextValue: () -> Bool?) { value = nextValue() } } + +// MARK: Extensions + + +/// Applies the given transform if the given condition evaluates to `true`. +/// - Parameters: +/// - condition: The condition to evaluate. +/// - transform: The transform to apply to the source `View`. +/// - Returns: Either the original `View` or the modified `View` if the condition is `true`. +@ViewBuilder func `if`(_ condition: Bool, transform: (Self) -> Content) -> some View { + if condition { + transform(self) + } else { + self + } +} From 6689d4d75091563ebfe7594aa01b32bc44d696da Mon Sep 17 00:00:00 2001 From: Kevin Barnes Date: Sat, 27 May 2023 17:30:07 -0400 Subject: [PATCH 2/3] Put View+If in view extension --- Sources/SwipeActions.swift | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Sources/SwipeActions.swift b/Sources/SwipeActions.swift index 2bd578d..ede7ca8 100644 --- a/Sources/SwipeActions.swift +++ b/Sources/SwipeActions.swift @@ -1448,15 +1448,17 @@ struct AllowSwipeToTriggerKey: PreferenceKey { // MARK: Extensions -/// Applies the given transform if the given condition evaluates to `true`. -/// - Parameters: -/// - condition: The condition to evaluate. -/// - transform: The transform to apply to the source `View`. -/// - Returns: Either the original `View` or the modified `View` if the condition is `true`. -@ViewBuilder func `if`(_ condition: Bool, transform: (Self) -> Content) -> some View { - if condition { - transform(self) - } else { - self +extension View { + /// Applies the given transform if the given condition evaluates to `true`. + /// - Parameters: + /// - condition: The condition to evaluate. + /// - transform: The transform to apply to the source `View`. + /// - Returns: Either the original `View` or the modified `View` if the condition is `true`. + @ViewBuilder func `if`(_ condition: Bool, transform: (Self) -> Content) -> some View { + if condition { + transform(self) + } else { + self + } } } From 602d2805960932051cb333bf05ad85f5e5513508 Mon Sep 17 00:00:00 2001 From: Kevin Barnes Date: Fri, 8 Nov 2024 12:05:41 -0500 Subject: [PATCH 3/3] Support visionOS --- Sources/SwipeActions.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/SwipeActions.swift b/Sources/SwipeActions.swift index ede7ca8..09e37a9 100644 --- a/Sources/SwipeActions.swift +++ b/Sources/SwipeActions.swift @@ -479,8 +479,10 @@ public struct SwipeView: View where Labe leadingState == nil && newValue == .triggering if changed, options.enableTriggerHaptics { /// Generate haptic feedback if necessary. + #if !os(visionOS) let generator = UIImpactFeedbackGenerator(style: .rigid) generator.impactOccurred() + #endif } } .onChange(of: trailingState) { [trailingState] newValue in @@ -490,8 +492,10 @@ public struct SwipeView: View where Labe trailingState == nil && newValue == .triggering if changed, options.enableTriggerHaptics { + #if !os(visionOS) let generator = UIImpactFeedbackGenerator(style: .rigid) generator.impactOccurred() + #endif } }