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

Ability for GestureButton to gracefully prioritise handling between a tap and long press action #15

Open
bardigolriz opened this issue Jan 4, 2023 · 1 comment

Comments

@bardigolriz
Copy link

I have a GestureButton that I have events for when the user taps as well as long presses (with a minimum delay).

I can’t depend on plain SwiftUI gesture modifiers for this because when the two are chained together, the long press minimum duration set appears to be ignored.

With GestureButton, I am trying to use releaseInsideAction combined with longPressAction with a longPressDelay of 0.1.

When I tap on the button, the releaseInsideAction is called correctly.

When I long press on the button, the longPressAction after the correct delay is called correctly. However, it also then proceeds to call the releaseInsideAction too, which isn’t what I want or expected.

To workaround this, I have added a new @State variable:

private var isLongPressed: Bool = false

Inside tryTriggerLongPressAfterDelay(), after its action() call, I flag it to be true:

        DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
            guard self.longPressDate == date else { return }
            action()
            isLongPressed = true
        }

Finally, in tryHandleRelease(), I check whether isLongPressed is true to determine whether to proceed with its associated action:

        if !isLongPressed {
            if geo.contains(value.location) {
                releaseInsideAction?()
            } else {
                releaseOutsideAction?()
            }
        }
        else {
            isLongPressed = false
        }
        endAction?()

In my tests, this seems to work really well without any issues. Can’t see where it could possibly trip up! Thank you so much for implementing such a versatile button that helps workaround SwiftUI limitations.

@danielsaidi
Copy link
Owner

danielsaidi commented Jan 4, 2023

Thank you @bardigolriz

Although I aimed for GestureButton to be pretty straightforward, I can see the use case where you would not want to trigger the release action after the long press.

I will try to find time to add this to the two buttons 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants