gpt4 book ai didi

SwiftUI - 很难在自定义 View 之上滚动

转载 作者:行者123 更新时间:2023-12-03 19:11:14 25 4
gpt4 key购买 nike

我在我的应用程序中有一个 View ,它显示了一个用于显示信息的自定义单元格数组的 ForEach,但我注意到很难在这些单元格的顶部滚动。我注意到如果我删除 .gesture(LongPressGesture()),它工作得很好,但我想保留它。有没有其他方法可以解决这个问题或替代 .gesture(LongPressGesture())?

这是我的代码:

struct refrigeItem:  Identifiable, Hashable {
var id = UUID()
var icon: String
var title: String
var daysLeft: Int
}




struct RefrigeratorItemCell: View {
var icon: String
var title: String
var lastsUntil: Int


var body: some View {
HStack {
Text(icon)
.font(.largeTitle)
.padding(.leading, 8)
VStack {
HStack {
Text(title)
.font(.custom("SF Pro Text", size: 16))
.multilineTextAlignment(.leading)
Spacer()
}

HStack {
//TODO: fix this
Text("lasts for \(self.lastsUntil) days")
.font(.custom("SF Compact Display", size: 16))
.foregroundColor(.gray)
.multilineTextAlignment(.leading)
Spacer()
}
}
Spacer()

}
.padding()
.background(Rectangle().cornerRadius(16).padding(.horizontal)
.foregroundColor(.gray)
)
.padding(.bottom)


}
}

struct ContentView: View {
@State var displayPreview = [refrigeItem(icon: "🥚", title: "eggs", daysLeft: 5), refrigeItem(icon: "🥐", title: "croissant", daysLeft: 6)]
var body: some View{
NavigationView {
GeometryReader { geo in
VStack {
ScrollView(.vertical, showsIndicators: true, content: {
VStack {
ForEach(self.displayPreview, id: \.self){item in
RefrigeratorItemCell(icon: item.icon, title: item.title, lastsUntil: item.daysLeft)
.gesture(LongPressGesture()
.onEnded({ i in
print("long pressed")
}))
}
}
})
}
}
}
}
}

最佳答案

更新您的 RefrigeratorItemCell在添加长按手势之前添加 onTapGesture:

      RefrigeratorItemCell(icon: item.icon, title: item.title, lastsUntil: item.daysLeft)
.onTapGesture {}
.gesture(LongPressGesture()
.onEnded({ i in
print("long pressed")
}))

关于SwiftUI - 很难在自定义 View 之上滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62052417/

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