7

I'm in a pickle with SwiftUI to where I have a vertical ScrollView consisting of buttons stacked vertically. When I start scrolling, there's two outcomes:

  1. If I started scrolling when my finger was on the button, when the scroll stops, the button action fires.
  2. If I started scrolling when my finger was on the padding between the buttons, when the scroll stops, not action occurs.

My question: is there any way to prevent the button action from firing in scenario (1.) above so i.e. the button is disabled or the action is only allowed to fire when the scroll is stopped/motionless?

ScrollView(.vertical) {

    ForEach(0...5, id: \.self) { i in

        VStack(alignment: .leading, spacing: 15) {

            Button(action: {
                print("selected")
            }) {
                Text("Button \(i)")
            }
        }
    }
}

I've tried tagging the .gesture() after the code block, but it only recognizes DragGesture().onChanged() and not DragGesture().onEnded(). I've considered trying to detect the location of the drag start and cross-reference that point with the position of all button on the screen, but that seems a little excessive.

Any help, suggestions, or alternatives are appreciated!

2 Answers 2

4

Try to use view instead of a Button. With tap gesture and (maybe) gesture mask.

Sign up to request clarification or add additional context in comments.

7 Comments

Could you please add in few lines of code as well, to support your answer?
@RohitNimmala, I think @orlangur meant something like Text("Button \(i)).onTapGesture{ print("selected") } instead of Button(action: {...}) { Text(...)}
Great solution! I should've considered that, but you'd think you'd want to use a Button for it's intended purpose you know...
What about if I am using NavigationLink instead of a Button, it is triggered when scrolling when it shouldn’t. Thanks!
@jberlana NavigationLink seem working fine without any additional adjustments. I tried this code: twitter.com/sergeygarazha/status/1258712441623580673?s=20
|
3

The problem appears to be only when you have the scrollview inside a modal presented screen. In case of the pushed screen, the button action doesn't get fired. For me it looks like an iOS bug.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.