如何解决使用DragGesture时ScrollView无法正常工作
我正在为LazyVGrid使用ScrollView。为了达到按下按钮的效果,我正在使用DragGesture。但是ScrollView无法正确滚动。
视觉表示:
这是我的代码:
struct ShowHint: View {
@State var pressed: Int = -1
var columns: [GridItem] = Array(repeating: .init(.flexible()),count: 5)
var body: some View {
ZStack{
ScrollView(showsIndicators: false) {
LazyVGrid(columns: columns,spacing: 30) {
ForEach(0..<500) { i in
Text("\(i)")
.padding(.vertical,10)
.frame(maxWidth: .infinity)
.background(Color.red.opacity(pressed == i ? 0.5 : 1))
.gesture(DragGesture(minimumdistance: 0)
.onChanged() { _ in
pressed = pressed == i ? -1 : i }
.onEnded { _ in
pressed = -1
})
.overlay(Group {
if pressed == i {
ShowOnTopOfButton(theS: "\(i)")
.offset(y: -50.0)
.allowsHitTesting(false)
}})
}
}
}
.padding(.top,50)
.padding(.horizontal,10)
}
}
}
struct ShowOnTopOfButton: View {
var theS: String
var body: some View {
vstack {
Text("\(theS)")
.padding(15)
.background(Color.blue)
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。