Adding a drag gesture in SwiftUI to a View inside a ScrollView blocks the scrolling

后端 未结 7 537
忘掉有多难
忘掉有多难 2020-12-23 12:49

So I have a ScrollView holding a set of views:

    ScrollView {
        ForEach(cities) { city in
            NavigationLink(destination: ...) {         


        
7条回答
  •  死守一世寂寞
    2020-12-23 13:48

    I have created an easy to use extension based on the Michel's answer.

    struct NoButtonStyle: ButtonStyle {
        func makeBody(configuration: Configuration) -> some View {
            configuration.label
        }
    }
    
    extension View {
        func delayTouches() -> some View {
            Button(action: {}) {
                highPriorityGesture(TapGesture())
            }
            .buttonStyle(NoButtonStyle())
        }
    }
    

    You apply it after using a drag gesture.

    Example:

    ScrollView {
        YourView()
            .gesture(DragGesture(minimumDistance: 0)
                .onChanged { _ in }
                .onEnded { _ in }
            )
            .delayTouches()
    }
    

提交回复
热议问题