Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable animation in list when observed object changes in SwiftUI?

Tags:

ios

swiftui

How do I disable animation when view model data changes?

I have the following code:

struct FormView: View {

    @ObservedObject var viewModel: FormViewModel

    var body: some View {
        List {
            ForEach(viewModel.options) { option in
                Text(option.displayValue)
            }
        }
    }
}

Every time view model changes List gets updated with animation.
How can I disable it?
I tried adding .animation(nil) but it does not help

like image 488
0rt Avatar asked Nov 23 '19 23:11

0rt


People also ask

How do I turn off animation in SwiftUI?

You can no longer disable animations in SwiftUI using the deprecated animation(nil) modifier. We can use the transaction modifier to create the same result and disable animations for a specific view in SwiftUI. If you like to improve your SwiftUI knowledge even more, check out the SwiftUI category page.

Does SwiftUI use Core Animation?

SwiftUI uses Core Animation for rendering by default, and its performance is great for most animations. But if you find yourself creating a very complex animation that seems to suffer from lower framerates, you may want to utilize the power of Metal, which is Apple's framework used for working directly with the GPU.

How do I turn off animation in FLutter?

There is no way to disable Transition animations and Slide Animations on FLutter, but this a requested feature right now due to Flutter Web being a thing now. As a workaround, you can use the transition-duration property of PageRouteBuilder Widget. Navigator.

What is SwiftUI animation?

SwiftUI includes basic animations with predefined or custom easing, as well as spring and fluid animations. You can adjust an animation's speed, set a delay before an animation starts, or specify that an animation repeats.


2 Answers

The solution I found is to add a unique identifier that changes every time, so it will rebuild the list every time without animation. Verified on iOS 13.4.

var body: some View {
    List {
        ForEach(viewModel.options) { option in
            Text(option.displayValue)
        }
    }
    .id(UUID()) // no animation
}
like image 77
Burgler-dev Avatar answered Oct 10 '22 01:10

Burgler-dev


I think the best solution is to set the UUID() as the animation value input:

        .animation(nil, value: UUID())
like image 38
Azhman Adam Avatar answered Oct 10 '22 00:10

Azhman Adam