我正在尝试在同一列表视图的单元格之间传递视图。我在网上看到了 0 个例子。每个示例都是为了让 ForEach 显示视图。
我知道我可以使用 ScrollView,但我对学习使用 List 视图特别感兴趣。
这是我的单元格的代码:
struct SelectableRow: View {
@Binding var selectedCategoryID: Int
@Binding var previousSelectedCategoryID: Int
var category: Category
@Namespace private var namespace
var body: some View {
HStack {
Text(self.category.name.capitalizingFirstLetter())
.font(.body)
.fontWeight(.light)
Spacer()
.background(Color.red)
if category.id == selectedCategoryID {
Circle()
.foregroundColor(.pink)
.aspectRatio(contentMode: .fit)
.frame(height: 30)
.matchedGeometryEffect(id: "Cell\(selectedCategoryID)", in: namespace, properties: .frame, anchor: .center, isSource: false)
}
else if category.id == previousSelectedCategoryID {
Circle()
.foregroundColor(.pink)
.aspectRatio(contentMode: .fit)
.frame(height: 30)
.matchedGeometryEffect(id: "Cell\(selectedCategoryID)", in: namespace, properties: .frame, anchor: .center, isSource: true)
}
}
.contentShape(Rectangle())
.onTapGesture {
previousSelectedCategoryID = selectedCategoryID
withAnimation {
selectedCategoryID = category.id
}
}
}
}
它不起作用..当我实际上希望将视图从一个单元格动画传递到另一个单元格时,它向我显示了选定单元格和前一个选定单元格的视图。
有人知道我在做什么错吗?