我的项目中有很多可重用的视图修改器,但我从来没有能够制作一个接受任何对象而不是特定对象的视图修改器。
外汇。在下面的 viewmodifier 中,我如何让它接受任何对象而不仅仅是“StopContent”,这样我每次想在新对象上使用它时就不必编写新的 viewModifier?
struct DragToDeleteContent: ViewModifier {
let stopContent:StopContent
@Binding var contentArray: [StopContent]
@State private var deleted:Bool = false
func body(content: Content) -> some View {
return content
.dragToDelete(deleted: $deleted)
.onChange(of: deleted, perform: { deleted in
if deleted { delete() }
})
}
func delete() {
if let arrayIndex = contentArray.firstIndex(of: stopContent) {
contentArray.remove(at: arrayIndex)
}
}
}