As we can see, now the change in width gets animated for the first view. This illustrates how the .animation() modifier works: it allows us to declare a scoped animation that will only apply to the view to which it's been attached. Now let's talk about the function withAnimation(). We've seen that the modifier .animation() allows us to declare scoped animations. However, there are situations where we need the opposite: we want to update some state and animate everything that depends on that state that can be animated. This is exactly what withAnimation() does: if we use it to wrap the code that updates the state of the views, then we see that both views now animate their change in width. |