SwiftUI在拖动时禁用按钮上的点击手势

w1jd8yoj  于 2022-10-31  发布在  Swift
关注(0)|答案(1)|浏览(218)

我有一个“caroussel视图”,其中有不同的“卡”,与按钮。我想禁用按钮,而用户拖动caroussel视图。

....     
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))
                    })
            )
}
....
6qftjkof

6qftjkof1#

在您的程式码中,将.disabled修饰词加入至按钮,如下所示,并加入@State var

struct ContentView: View {

    @State private var buttonDisabled = true

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

DragGesture-〉.onChanged的内部,生成buttonDisabled = true;在DragGesture-〉.onEnded的内部,生成buttonDisabled = false

相关问题