Disable Animations On A Specific View in SwiftUI Using Transactions
Disable Animations On A Specific View in SwiftUI Using Transactions
53,093 followers
A weekly blog about Swift, iOS and Xcode Tips and Tricks
Every Tuesday, curated Swift content from the community for free.
Your email address Follow
The animation(nil) view modifier is deprecated, and you should no longer use it.
The deprecation warning makes it clear to us that we should try to use this
method no longer. Since it’s deprecated, we’ll sooner or later have to replace this
code, and we also want a warning-free project.
The suggestion is to use the animation(_:value:) modifier instead. It wasn’t
immediately clear how the old view modifier would transform into this new one.
Once again, suggested by others, I tried out the following modifier:
.animation(nil, value: UUID())
I couldn’t make this solution work, and I wouldn’t say I liked it. A view redraw will
update the UUID, which means our modifier might run for no reason. Using a
UUID in this way also makes it feel like a hack to me. Therefore, I continued my
journey.
Any adjustments we make to the given transaction only apply to the animations
used within the view containing the modifier.
The final result looks much better to me!
Conclusion
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. Feel free to contact me or tweet me on Twitter if you have any
additional tips or feedback.
Thanks!