TapGesture and Button is not working in list if adding simultaneousGesture with minimumDistance: 0.0 - SwiftUI

608 views Asked by At

I need to use a List for reusability that it provided and disable the scroll based on adding simultaneousGesture with DragGesture(minimumDistance: 0.0) to support iOS 15. scrollDisabled is only for iOS 16.

So when adding the gesture to the list onTapGesture and Button is not working inside each row of my list.

Here is a sample code:

        ZStack {
        List {
            ForEach(0..<10, id:\.self) { index in
                VStack {
                    Text("Sample text \(index)")
                        .onTapGesture {
                            print("Sample Text tapped \(index)")
                        }
                    
                    Button("Sample button \(index)") {
                        print("Sample button tapped \(index)")
                    }
                    .buttonStyle(.bordered)
                    .frame(height: 50)
                }
                .frame(height: 100)
            }
        }
        .simultaneousGesture(
            DragGesture(minimumDistance: 0.0)
                .onChanged({ _ in
                    print("Drag onChanged")
                })
                .onEnded({ _ in
                    print("Drag onEnded")
                })
        )
    }

if we set the minimumDistance to 0.1 tapGesture and button will work, but the scroll is not disabled anymore.

Do you have any idea or workaround? Thanks.

1

There are 1 answers

1
Alex Luque On

I'm not sure if I understood your problem correctly. I guess what you want to do is to keep onTapGesture, Button and List's scroll functionalities all them working, except when a drag event occurs.

If you place the simultaneousGesture in the ForEach instead of assigning it to the List, then they all work, except when you drag over the Text or Button elements. In this case, the scroll does not work.

struct ContentView: View {
    var body: some View {
        ZStack {
            List {
                ForEach(0..<10, id:\.self) { index in
                    VStack {
                        Text("Sample text \(index)")
                            .onTapGesture {
                                print("Sample Text tapped \(index)")
                            }
                        
                        Button("Sample button \(index)") {
                            print("Sample button tapped \(index)")
                        }
                        .buttonStyle(.bordered)
                        .frame(height: 50)
                    }
                    .frame(height: 100)
                }
                .simultaneousGesture(
                    DragGesture(minimumDistance: 0.0)
                        .onChanged { _ in
                            print("Drag onChanged")
                        }
                        .onEnded { _ in
                            print("Drag onEnded")
                        }
                )
            }
        }
    }
}

enter image description here