SwiftUI List NavigationView onDelete 警报确认有丑陋的动画

Gle*_*enn 5 ios swift swiftui

问题是,每当你将列表放入navigationView中时,列表行的删除取消的动画都不太好看。body我在我的财产上做错了什么吗?

var body: some View {
    NavigationView {
        VStack {
            List {
                ForEach(self.contacts){ contact in
                    ContactRow(contact: contact)
                }.onDelete { self.setDeletIndex(at: $0) }
            }
            .alert(isPresented: $showConfirm) {
                Alert(title: Text("Delete"), message: Text("Sure?"),
                      primaryButton: .cancel(),
                      secondaryButton: .destructive(Text("Delete")) {
                        
                        self.delete()
                      })
            }
            .listStyle(PlainListStyle())
            .navigationTitle("Contacts")
            .navigationBarItems(trailing: HStack {
                Button("Add", action: self.addItem)
            })
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

emr*_*pun 1

虽然回复晚了,但可能会对将来的人有所帮助。

此解决方案需要 iOS 15.0 及更高版本,并且您的Contact模型符合Hashable.

onDelete我们可以通过完全停止使用该方法并使用它来摆脱这种奇怪的动画swipeActions。我们可以这样做:

    List {
        ForEach(Array(contacts.enumerated()), id: \.element) {
            index, contact in
            ContactRow(contact: contact)
                .swipeActions {
                    Button(action: {
                        indexToDelete = index
                        showConfirm = true
                    }) {
                        Text("Delete")
                    }
                    .tint(.red)
                }
        }
    }
    .alert(isPresented: $showConfirm) {
        Alert(
            title: Text("Delete"),
            message: Text("Sure?"),
            primaryButton: .cancel(),
            secondaryButton: .destructive(Text("Delete")) {
                self.delete()
            }
        )
    }
Run Code Online (Sandbox Code Playgroud)