Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How update a SwiftUI List without animation

Tags:

ios

swift

swiftui

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.

like image 432
flashspys Avatar asked Jun 10 '19 21:06

flashspys


2 Answers

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()
like image 69
Gene Z. Ragan Avatar answered Sep 25 '22 22:09

Gene Z. Ragan


on tvOS this works for me:

List {
    ...
}
    .animation(.none)
like image 41
Shaybc Avatar answered Sep 24 '22 22:09

Shaybc