0

I'm building an app that would work better with the ability to recognize multitouch gestures (e.g. double click, triple click, and so on - ...clicks with multiple fingers). As of now, the app (or specifically a SwiftUI button) recognizes two gestures, single click and a long press. Is there an easy way to add an action to the button after a double click or a triple click on it?

Button(action: {
    // action after single click              
}, label: {
    Text("Button")
}).simultaneousGesture(LongPressGesture(minimumDuration: 1).onEnded { _ in
    isLongPressing = true
                    
    // action after long press for 1 second
})

... here's a snippet to give you an idea of how the button works in the UI. Did some research on how it could be done (this question dives into a similar if not the same topic), but couldn't seem to find a way that works for me. Thanks in advance!

scraptechguy
  • 69
  • 10
  • Can you not just add another simultaneous gesture? Using a tap that requires multiple taps? Also, I don’t know if it’s a good idea to use a button for this? I’d probably handle all the gestures myself rather than use the button for the tap. – Fogmeister Nov 17 '22 at 12:57
  • Yeah, that came across my mind too, but didn't find a way to incorporate that. Do you know how it could look like? And good point with the button by the way – scraptechguy Nov 17 '22 at 13:02
  • 1
    So, the `.simultaneousGesture` just returns another view. So you can use it multiple times. Add multiple `.simultaneousGesture` with each one containing a different gesture. It’s not something that can only be used once. Just like how you can have multiple `.padding` etc… I believe you put them in the order of cancellation. But you can play around with that. – Fogmeister Nov 17 '22 at 14:19
  • I'll see what I can do. Thanks mate! – scraptechguy Nov 17 '22 at 15:05
  • Good luck. If I get home to my computer I’ll have a play and see what I can come up with and add an answer. – Fogmeister Nov 17 '22 at 15:58
  • Thanks again, would love that – scraptechguy Nov 17 '22 at 20:03
  • Hmm... this is more difficult than I feel it should be. I can get two different types of gesture to work but I can't consistently get a tap and a double tap to work. – Fogmeister Nov 17 '22 at 20:51
  • Right? One would expect it to be a single easily reachable line of code... – scraptechguy Nov 17 '22 at 21:01
  • @Fogmeister, I didn't get much further with the whole gesture thing, did you get around to try something? – scraptechguy Nov 19 '22 at 21:37

0 Answers0