Skip to content
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
57 changes: 32 additions & 25 deletions Sources/SwipeActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public struct SwipeOptions {
var swipeEnabled = true

/// The minimum distance needed to drag to start the gesture. Should be more than 0 for best compatibility with other gestures/buttons.
var swipeMinimumDistance = Double(2)
var swipeMinimumDistance = Double(20)

/// The style to use (`mask`, `equalWidths`, or `cascade`).
var actionsStyle = SwipeActionStyle.mask
Expand Down Expand Up @@ -403,8 +403,16 @@ public struct SwipeView<Label, LeadingActions, TrailingActions>: View where Labe
self.leadingActions = leadingActions
self.trailingActions = trailingActions
}

public var body: some View {
let dragGesture = DragGesture(minimumDistance: options.swipeMinimumDistance)
.updating($currentlyDragging) { value, state, transaction in
state = true
}
.onChanged(onChanged)
.onEnded(onEnded)
.updatingVelocity($velocity)

HStack {
label()
.offset(x: offset) /// Apply the offset here.
Expand Down Expand Up @@ -438,17 +446,7 @@ public struct SwipeView<Label, LeadingActions, TrailingActions>: View where Labe
)

// MARK: - Add gestures

.highPriorityGesture( /// Add the drag gesture.
DragGesture(minimumDistance: options.swipeMinimumDistance)
.updating($currentlyDragging) { value, state, transaction in
state = true
}
.onChanged(onChanged)
.onEnded(onEnded)
.updatingVelocity($velocity),
including: options.swipeEnabled ? .all : .subviews /// Enable/disable swiping here.
)
.highPriorityGesture(dragGesture, including: options.swipeEnabled ? .all : .subviews)
.onChange(of: currentlyDragging) { currentlyDragging in /// Detect gesture cancellations.
if !currentlyDragging, let latestDragGestureValueBackup {
/// Gesture cancelled.
Expand Down Expand Up @@ -501,17 +499,23 @@ public struct SwipeView<Label, LeadingActions, TrailingActions>: View where Labe
}
.onChange(of: swipeViewGroupSelection.wrappedValue) { newValue in
if swipeViewGroupSelection.wrappedValue != id {
currentSide = nil

if leadingState != .closed {
leadingState = .closed
close(velocity: 0)
}

if trailingState != .closed {
trailingState = .closed
close(velocity: 0)
}
reset()
}
}
}

func reset() {
withAnimation {
currentSide = nil

if leadingState != .closed {
leadingState = .closed
close(velocity: 0)
}

if trailingState != .closed {
trailingState = .closed
close(velocity: 0)
}
}
}
Expand Down Expand Up @@ -1282,6 +1286,7 @@ public extension AnyTransition {
active: SwipeDeleteModifier(visibility: 0),
identity: SwipeDeleteModifier(visibility: 1)
)
.combined(with: .move(edge: .leading))
}
}

Expand Down Expand Up @@ -1385,7 +1390,9 @@ struct GestureVelocity: DynamicProperty {
extension Gesture where Value == DragGesture.Value {
func updatingVelocity(_ velocity: GestureVelocity) -> _EndedGesture<_ChangedGesture<Self>> {
onChanged { value in
velocity.update(value)
withAnimation {
velocity.update(value)
}
}
.onEnded { _ in
velocity.reset()
Expand Down