gpt4 book ai didi

SwiftUI Tap Gesture 在 ScrollView 中不起作用

转载 作者:行者123 更新时间:2023-12-04 09:30:44 24 4
gpt4 key购买 nike

我正在画盒子,我想点击它们。但是,如果我将代码放在 ScrollView 中,则点击手势不起作用。如果我删除 GeometryReaer 代码可以工作,但是我需要它(虽然我没有在这个简单的例子中使用它)。有什么想法可以让代码与 ScrollViewGeometryReader 一起工作吗?

enter image description here

struct ContentView: View {
var body: some View {
// REMOVING ScrollView makes TapGesture WORK
ScrollView {
WorksGridView()
}
}
}

struct WorksGridView: View {
private var items = 5

var body: some View {
GeometryReader { geometry in
VStack(spacing: 0) {
ForEach(0..<self.items) { index in
GridInventoryItem()
.onTapGesture {
print("I'm not appearing!!!")
}
}
}
}
}
}

struct GridInventoryItem: View {
var body: some View {
Rectangle()
.padding()
.frame(width: 150, height: 150)
}
}

最佳答案

将 ScrollView 移到 Geometry Reader 中,它就可以工作了:在 iOS 13.5 上测试

struct ContentView: View {
var body: some View {
// REMOVING ScrollView makes TapGesture WORK

WorksGridView()

}
}

struct WorksGridView: View {
private var items = 5

var body: some View {
GeometryReader { geometry in
ScrollView{
VStack(spacing: 0) {
ForEach(0..<self.items) { index in
GridInventoryItem()
.onTapGesture {
print("I'm not appearing!!!")
}
}
}
}
}
}
}

struct GridInventoryItem: View {
var body: some View {
Rectangle()
.padding()
.frame(width: 150, height: 150)
}
}

关于SwiftUI Tap Gesture 在 ScrollView 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62864858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com