简体   繁体   中英

How update a SwiftUI List without animation

I want to update a SwiftUI List without any insert animation. My List is getting its data from an @EnvironmentObject I already tried to wrap the List itself and the PassthroughSubject.send() in a withAnimation(.empty) block but this does not help.

A very very dirty workaround is to call UIView.setAnimationsEnabled(false) (yes, UIKit has impact on SwiftUI), but there must be a SwiftUI-like way to set custom insert animations.

This works, just place .id(UUID()) at the end of your list

List {
    // for each etc
}.id(UUID())

Sort of like reloadData for UIKit

While the answer provided by DogCoffee works, it does so in an inefficient manner. Sometimes we do have to force the system to do what we want by being inefficient. In the case of implicit animations in SwiftUI, there is a better way to disable them.

Using the Transaction mechanism in SwiftUI, we can define an extension that can be applied to any view. This will disable animations for the view and any children.

For the list view example, this avoids replacing all the data in the list with a new, but identical copies.

extension View {
     
    func animationsDisabled() -> some View {
        return self.transaction { (tx: inout Transaction) in
            tx.disablesAnimations = true
            tx.animation = nil
        }.animation(nil)
    }
}

Try applying this extension to your list, or the parent containing view. You may have to experiment to find which view is ideal.

List {
    // for each etc
}.animationsDisabled()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM