我有一个“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))
})
)
}
....
1条答案
按热度按时间6qftjkof1#
在您的程式码中,将
.disabled
修饰词加入至按钮,如下所示,并加入@State var
。在
DragGesture
-〉.onChanged
的内部,生成buttonDisabled = true
;在DragGesture
-〉.onEnded
的内部,生成buttonDisabled = false
。