How to make Buttons detect tap, double tap and long press gestures in SwiftUI

Federica Benacquista
3 min readAug 7, 2023

Ahhh taps, such an easy task. Well at least until making them work properly confused the hell out of me.

So… I see you want to code a juicy multitasking button that does an endless amount of things depending on how you touch it, yet WITHOUT sacrificing the basic Button functionality (e.g. highlight, fade out and all of that nice stuff buttons do without you asking). Something tells me you want it to perform different actions whether you tap it or press it, maybe you also want an action for a double tap, am I right?

Well, don’t you worry my child, your beloved Swift Queen is here to rescue you -again.

What you need to do is the following:

Button {
//This stays empty no matter what.
} label: {
Text("You love me")
}
//This is outside the label
.simultaneousGesture(LongPressGesture().onChanged { _ in
print("Taaap started")
})
.simultaneousGesture(LongPressGesture().onEnded { _ in
print("Taaap ended")
})
.simultaneousGesture(TapGesture(count: 2).onEnded {
print("Tap tap")
})
.simultaneousGesture(TapGesture().onEnded {
print("Tap")
})

NOTE:

--

--

Federica Benacquista

I’m a self taught iOS developer and soon to be entrepreneur. I like to explain tricky concepts as easily as possible and it’s exactly what I do here.