GestureButton can trigger many different actions with a single gesture.
You can use a GestureButton just like a regular Button, and can define different actions for different gestures:
struct ContentView: View {
@State private var isPressed = false
var body: some View {
GestureButton(
isPressed: $isPressed,
pressAction: { print("Pressed") },
releaseInsideAction: { print("Released Inside") },
releaseOutsideAction: { print("Released Outside") },
longPressAction: { print("Long Pressed") },
doubleTapAction: { print("Double Tapped") },
repeatAction: { print("Repeating Action") },
dragStartAction: { value in print("Drag Started") },
dragAction: { value in print("Drag \(value)") },
dragEndAction: { value in print("Drag Ended") },
endAction: { print("Gesture Ended") }
) { isPressed in
Color.yellow // You can use any button content view.
}
.gestureButtonConfiguration(...)
}
}You can pass in custom configurations to change how the button behaves, e.g. the max time between two taps for the taps to count as a double-tap. You can use any content View as the button label, based on the isPressed state.
GestureButton can be installed with the Swift Package Manager:
https://github.com/danielsaidi/GestureButton.git
You can become a sponsor to help me dedicate more time on my various open-source tools. Every contribution, no matter the size, makes a real difference in keeping these tools free and actively developed.
The online documentation has more information, articles, code examples, etc.
The Demo folder has a demo app that lets you explore the library and its components.
Feel free to reach out if you have questions or if you want to contribute in any way:
- Website: danielsaidi.com
- E-mail: daniel.saidi@gmail.com
- Bluesky: @danielsaidi@bsky.social
- Mastodon: @danielsaidi@mastodon.social
GestureButton is available under the MIT license. See the LICENSE file for more info.