我最近开始使用 SwiftUI。让我解释一下我的想法:我有一个视图,允许我将项目添加到列表中。该列表应显示在视图内。问题是我可以在内部显示它,但无法滚动它或执行滑动来触发 .onDelete 操作。我怎样才能实现这个目标?
这是带有列表的视图:
import SwiftUI
struct ListInsideScontrino: View {
@State private var isAddFoodViewPresented = false
@EnvironmentObject var foodData: FoodData
var foodItems: [MyFoodItem] {
foodData.foodItems
}
var body: some View {
if foodItems.isEmpty {
VStack {
Text("Il riepilogo del tuo ordine apparirà qui una volta aggiunte le varie pietanze.")
.font(.callout)
.foregroundColor(.gray)
.multilineTextAlignment(.center)
.padding()
Text("Clicca il bottone in basso a destra ed inizia ad aggiungere il cibo!")
.font(.callout)
.foregroundColor(.gray)
}
.padding()
} else {
List {
ForEach(foodData.foodItems) { foodItem in
FoodItemRow(foodItem: foodItem)
.listRowSeparator(.hidden)
}
.onDelete(perform: deleteFoodItem)
}
.listStyle(InsetGroupedListStyle())
.background(Color.gray.opacity(0.100))
}
Color.gray.opacity(0.100)
}
private func deleteFoodItem(at offsets: IndexSet) {
foodData.foodItems.remove(atOffsets: offsets)
}
}
#Preview {
ListInsideScontrino()
}
这是我想显示我的列表的视图:
import SwiftUI
struct ScontrinoView: View {
var body: some View {
ZStack {
ListInsideScontrino()
}
.padding()
.mask(
Path { path in
path.move(to: CGPoint(x: 0, y: 0))
for i in 0..<Int(UIScreen.main.bounds.height / 20) {
let x = CGFloat(i) * 20
path.addLine(to: CGPoint(x: x + 10, y: 30))
path.addLine(to: CGPoint(x: x + 20, y: 20))
}
path.addLine(to: CGPoint(x: UIScreen.main.bounds.width, y: UIScreen.main.bounds.height))
path.addLine(to: CGPoint(x: 0, y: UIScreen.main.bounds.height))
}
.scaleEffect(CGSize(width: 1.0, height: -1.0))
)
.shadow(radius: 5)
//.frame(maxWidth: .infinity, maxHeight: .infinity)
.cornerRadius(15)
.ignoresSafeArea()
}
}
我对这些错误表示歉意,我是意大利人,我可能写错了一些东西。
谢谢大家的回复!
我尝试在 ScrollView、ZStack ecc 之间交换视图。
这是一个解决方案
.mask
,只需将 scontrino 路径和列表放入ZStack
. 您只需使用以下命令使列表背景不可见.scrollContentBackground(.hidden)
意大利致敬