SwiftUI Animation - Spooky Action At A Distance

I have a grid-like container with subviews.

I recently changed some internal details of the subviews, so that changes to the values they display animate.

Now, the behaviour of the grid container has changed: the animation duration used for the internal changes is now also used when the grid is re-ordered or subviews are added or removed.

I can see why this happens: the grid repositions the subviews, and the subview has declared an animation that applies to all of its properties however they are modified.

This doesn't seem like a good idea to me. The principle of encapsulation suggests that I should be able to make internal changes to a component without suffering "spooky action at a distance", i.e. other components unexpectedly changing their behaviour.

Is this an inherent issue with SwiftUI animations, or does it suggest that I am doing something wrong?

You'll need to provide a sample project for us to understand the specifics of the issue.

That said, SwiftUI animations are state driven. So a good way to think about the effects of animations are:

  • You can animate the particular view when a specific value changes by applying the animation(_:value:) view modifier to the view.

  • Or you can animate all the visual changes when a state change occurs by changing the state inside of a call to the withAnimation(::) global function.

Depending on what you're trying to achieve here you'll be applying animation in either one of those ways.

SwiftUI Animation - Spooky Action At A Distance
 
 
Q