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 close swipe actions on label tap #7

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
42 changes: 42 additions & 0 deletions Sources/SwipeActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -408,6 +411,16 @@ public struct SwipeView<Label, LeadingActions, TrailingActions>: 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.
Expand Down Expand Up @@ -466,8 +479,10 @@ public struct SwipeView<Label, LeadingActions, TrailingActions>: 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
Expand All @@ -477,8 +492,10 @@ public struct SwipeView<Label, LeadingActions, TrailingActions>: View where Labe
trailingState == nil && newValue == .triggering

if changed, options.enableTriggerHaptics {
#if !os(visionOS)
let generator = UIImpactFeedbackGenerator(style: .rigid)
generator.impactOccurred()
#endif
}
}

Expand Down Expand Up @@ -1256,6 +1273,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.
Expand Down Expand Up @@ -1424,3 +1448,21 @@ struct AllowSwipeToTriggerKey: PreferenceKey {
static var defaultValue: Bool? = nil
static func reduce(value: inout Bool?, nextValue: () -> Bool?) { value = nextValue() }
}

// MARK: Extensions


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`<Content: View>(_ condition: Bool, transform: (Self) -> Content) -> some View {
if condition {
transform(self)
} else {
self
}
}
}