I'm struggling to implement TapGesture
and LongPressGesture
simultaneously in a ScrollView
. Everything works fine with .onTapGesture
and .onLongPressGesture
, but I want that the opacity of the button gets reduced when the user taps on it, like a normal Button()
.
However, a Button()
doesn't have an option to do something on a long press for whatever reason. So I tried to use .gesture(LongPressGesture() ... )
. This approach works and shows the tap indication. Unfortunately, that doesn't work with a ScrollView
: you can't scroll it anymore!
So I did some research and I found out that there has to be a TapGesture before the LongPressGesture
so ScrollView
works properly. That's the case indeed but then my LongPressGesture
doesn't work anymore.
Hope somebody has a solution...
struct ContentView: View {
var body: some View {
ScrollView(.horizontal){
HStack{
ForEach(0..<5){ _ in
Button()
}
}
}
}
}
struct Button: View{
@GestureState var isDetectingLongPress = false
@State var completedLongPress = false
var body: some View{
Circle()
.foregroundColor(.red)
.frame(width: 100, height: 100)
.opacity(self.isDetectingLongPress ? 0 : 1)
// That works, but there is no indication for the user that the UI recognized the gesture
// .onTapGesture {
// print("Tapped!")
// }
// .onLongPressGesture(minimumDuration: 0.5){
// print("Long pressed!")
// }
// The approach (*) shows the press indication, but the ScrollView is stuck because there is no TapGesture
// If I add a dummy TapGesture, the LongPressGesture won't work anymore but now the ScrollView works as expected
//.onTapGesture {}
// (*)
.gesture(LongPressGesture()
.updating(self.$isDetectingLongPress) { currentstate, gestureState,
transaction in
gestureState = currentstate
}
.onEnded { finished in
self.completedLongPress = finished
}
)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With