Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
device: 'iPhone 15'
- version: '18.6'
device: 'iPhone 16'
- version: '26.0'
- version: '26.0.1'
device: 'iPhone 17'

steps:
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ LongPressButton(minimumDuration: 0.5, maximumDistance: 10) {
}
```

## Known limitations

This package doesn't work with default buttons that are used inside of Lists. If you want to use this inside of a List apply a different style to the button. For example `plain` or `bordered`.

## Installation

### Swift Package Manager
Expand Down
63 changes: 14 additions & 49 deletions Sources/LongPressButton/LongPressButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,78 +9,43 @@ public struct LongPressButton<Label: View>: View {
private let label: Label
private let longPressActionName: Text?

@available(iOS, obsoleted: 26.0)
@State private var didLongPress = false
@available(iOS, obsoleted: 26.0)
@State private var longPressTask: Task<Void, Never>?

public var body: some View {
button
.accessibilityAction {
action()
}
.accessibilityAction(named: longPressActionName ?? Text("Alternative Action")) {
longPressAction()
}
}

@ViewBuilder
private var button: some View {
if #available(iOS 26.0, *) {
Button(action: {}) {
label
}
.simultaneousGesture(longPress.exclusively(before: tap))
} else {
Button(action: performActionIfNeeded) {
label
}
.onLongPressGesture(
maximumDistance: maximumDistance,
perform: {},
onPressingChanged: handleLongPress(isPressing:)
)
Button(action: performActionIfNeeded) {
label
}
.simultaneousGesture(longPress.simultaneously(with: detectRelease))
.accessibilityAction(named: longPressActionName ?? Text("Alternative Action")) {
longPressAction()
}
}

private var longPress: some Gesture {
LongPressGesture(minimumDuration: minimumDuration, maximumDistance: maximumDistance)
.onEnded { _ in
didLongPress = true
longPressAction()
}
}

private var tap: some Gesture {
TapGesture().onEnded {
action()
private var detectRelease: some Gesture {
DragGesture(minimumDistance: 0).onEnded { _ in
DispatchQueue.main.async {
// Mark long press as ended in next run loop to be sure that button action
// won't trigger together with it.
didLongPress = false
}
}
}

@available(iOS, obsoleted: 26.0)
private func performActionIfNeeded() {
longPressTask?.cancel()
if didLongPress {
didLongPress = false
} else {
action()
}
}

@available(iOS, obsoleted: 26.0)
private func handleLongPress(isPressing: Bool) {
longPressTask?.cancel()
guard isPressing else { return }
didLongPress = false
longPressTask = Task {
do {
try await Task.sleep(nanoseconds: UInt64(minimumDuration * 1_000_000_000))
} catch {
return
}
didLongPress = true
longPressAction()
}
}
}

// MARK: - Initialization
Expand Down
Loading