0

I have a "caroussel view", that has different "cards" in it, with buttons. I want to disable the buttons while user dragging the caroussel view.

....     
HStack(alignment: .center, spacing: spacing) {
                ForEach(cards.indices, id: \.self) { i in
                    cardView(card: cards[i], i: i)
                }
            }
            .frame(height: cardHeight + paddingTop)
            .padding(.horizontal, spacing)
            .offset(x: (CGFloat(currentIndex) * -width) + adjustmentWidth + offset)
            .gesture(
                DragGesture()
                    .updating($offset, body: { value, out, _ in
                        out = value.translation.width
                    })
                    .onEnded({ value in
                        offsetTop = 0
                        calcDragVelocityProgress(progress: value.translation.width, velocity: value.velocity.width, width: width)
                        animateIndex()
                    })
                    .onChanged({ value in
                        makeProgress(translationWidth: value.translation.width, width: width)
                        offsetTop = min(paddingTop, ((abs(offset) * (paddingTop * 2)) / width))
                    })
            )
}
....
Marta Paniti
  • 152
  • 6

1 Answers1

1

In your code add .disabledmodifier to the button as follows and add a @State var.

struct ContentView: View {

    @State private var buttonDisabled = true

    var body: some View {
        Button(action: {
            //your action here
        }) {
            Text("CLICK ME!")
        }
        .disabled(buttonDisabled)
    }
}

Inside DragGesture -> .onChanged make buttonDisabled = true and inside DragGesture -> .onEnded make buttonDisabled = false.

udi
  • 3,672
  • 2
  • 12
  • 33