1

代码如下。我想在onDelete点击时删除最终的淡入淡出动画。

struct ContentView: View {
    @State var names = ["david", "john", "amber"]
    
    var body: some View {
        List {
            ForEach(names, id: \.self) { (item) in
                Text(item)
            }.onDelete(perform: self.deleteItem)
        }
    }
    
    private func deleteItem(at indexSet: IndexSet) {
        self.names.remove(atOffsets: indexSet)
    }
}

我尝试使用.animation()但没有任何效果

4

1 回答 1

0

一个可能的解决方案是强制重绘List

struct ContentView: View {
    @State private var names = ["david", "john", "amber"]
    @State private var listId = UUID()

    var body: some View {
        List {
            ForEach(names, id: \.self) { item in
                Text(item)
            }
            .onDelete(perform: deleteItem)
        }
        .id(listId)
    }

    private func deleteItem(at indexSet: IndexSet) {
        names.remove(atOffsets: indexSet)
        listId = UUID()
    }
}
于 2021-02-04T23:56:11.623 回答