Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
302 views
in Technique[技术] by (71.8m points)

swift - Is there any way to remove the fading animation on .onDelete SwiftUI

The code is below. I want to delete the final fading animation when onDelete is tapped.

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)
    }
}

I tried to use .animation() but none works

question from:https://stackoverflow.com/questions/66055582/is-there-any-way-to-remove-the-fading-animation-on-ondelete-swiftui

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A possible solution is to force redraw the 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()
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...