GestureButton

0.4.0

A SwiftUI button that can trigger actions for many different gestures.
danielsaidi/GestureButton

What's New

2025-04-17T14:47:05Z

This version replaces init parameters with a new configuration.

This version also makes it possible to configure a max drag distance after which long presses will cancel.

✨ Features

  • GestureButton will now read configurations from the environment.
  • GestureConfiguration is a new gesture button configuration struct.
  • GestureConfiguration has a new longPressMaxDragDistance property.

🗑️ Deprecations

  • GestureButton has temporary deprecations.

Project Icon

Version Swift 5.10 Swift UI Documentation MIT License

GestureButton

GestureButton is a SwiftUI button that can trigger many different gesture-specific 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.
        }
    }
}

You can pass in various delays and timeouts 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 View as the button label.

Installation

GestureButton can be installed with the Swift Package Manager:

https://github.com/danielsaidi/GestureButton.git

Getting Started

A GestureButton can be used just like a regular Button, as shown above, but needs some extra handling when used within a ScrollView.

struct ContentView: View {

    @StateObject private var scrollState = GestureButtonScrollState()
    
    var body: some View {
        ScrollView(.horizontal) {
            GestureButton(
                scrollState: scrollState,
                pressAction: { print("Pressed") },
                label: { isPressed in
                    isPressed ? Color.yellow : Color.gray
                    // You can use any button content view.
                }
            )
        }
        .scrollGestureState(scrollState)
    }
}

In iOS 17 and earlier, you have to pass in a GestureButtonScrollState into the GestureButton initializer, for the button to not block the scroll gesture.

In iOS 18 and later, you must pass in a GestureButtonScrollState and apply it to the scroll view as well.

Documentation

The online documentation has more information, articles, code examples, etc.

Demo Application

The Demo folder has a demo app that lets you explore the library and its components.

Support my work

You can sponsor me on GitHub Sponsors or reach out for paid support, to help support my open-source projects.

Your support makes it possible for me to put more work into these projects and make them the best they can be.

Contact

Feel free to reach out if you have questions or if you want to contribute in any way:

License

GestureButton is available under the MIT license. See the LICENSE file for more info.

Description

  • Swift Tools 6.0.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sat Jun 14 2025 20:04:38 GMT-0900 (Hawaii-Aleutian Daylight Time)