SwiftUI - 很难滚动到自定义视图

SwiftUI - Its hard to scroll onto of custom views

我的应用程序中有一个视图,其中显示了一组自定义单元格的 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")
          }))