我在Xcode 13.2上使用Swift 5,SwiftUI 3.0。
我有以下代码:
ZStack { ... custom button }
.gesture(LongPressGesture(minimumDuration: 0.5, maximumDistance: 30)
.onEnded { _ in
withAnimation(.easeInOut(duration: 1.0)) {
// code when ended...
}
}
.onChanged { _ in
withAnimation(.easeInOut(duration: 0.2)) {
// code when touched...
}
})让我们称其为CustomButton。
然后我得到了这个ScrollView和LazyVGrid:
ScrollView {
VStack(spacing: 15) {
let columns = Array(repeating: GridItem(.flexible(), spacing: 10), count: 1)
LazyVGrid(columns: columns,spacing: 15) {
CustomButton()
CustomButton()
// and then some more...
}
}
}现在,当我从onChange中删除CustomButton处理程序时,滚动操作就可以了。所以我猜,因为onChange()一点击按钮就会触发,所以它优先于ScrollView。但是,我需要在onChange()被触发后,或者在用户触摸按钮之后发生的动画。
我已经尝试过在.onTapGesture {}修饰符之前有一个空的.gesture()。我尝试过将.gesture更改为.simulatenousGesture,并尝试了在SO或AppleDev论坛上找到的所有内容。
我想知道在ScrollView中是否可以有一个按钮,即:
.gesture().onChange
minimumDuration触摸,或者在ScrollView中仍然可以滚动
任何帮助都将不胜感激,谢谢!
发布于 2022-02-27 09:49:25
这种变化是有效的(不要问我为什么.):
struct CustomButton: View {
var body: some View {
RoundedRectangle(cornerRadius: 15)
.frame(height: 50)
.onTapGesture {
print("tapped")
}
.onLongPressGesture(minimumDuration: 0.5, maximumDistance: 30) {
print("longpress ended")
} onPressingChanged: { pressing in
print("longpress changed")
}
}
}https://stackoverflow.com/questions/71281713
复制相似问题