Is it possible in SwiftUI to set a button with an action on tap and a different action on long press ?
I have tried this:
Button(action: {
self.handleButtonTap()
})
{
Text("My nice button")
.foregroundColor(.primary)
}
.onLongPressGesture {
print("Long pressed!")
}
or instead of:
.onLongPressGesture {
print("Long pressed!")
}
using this:
.gesture(longPress)
where long press is something like:
var longPress: some Gesture {
....
}
But nothing seems to work. At best I have been able to attach the long press gesture to the Text of the button, but even in that case the normal tap cease to work.
Any good advice will be highly appreciated.
Please check if this works for you:
Button("Button") {
print("tapped")
}
.simultaneousGesture(LongPressGesture().onEnded { _ in
print("long pressed")
})
Please note that tap action is executed after every long press in the above code. You can handle this with a simple Bool.