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
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.
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.
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.
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.
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
}
I think the best solution is to set the UUID() as the animation value input:
.animation(nil, value: UUID())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With