@joe has a question about animation...
I am interested in [snip]— the location of the animation modifier:
Here's a way to visualize your question. Grab two sheets of paper. On one, write hasFadedIn = false
at the top. On the other write hasFadedIn = true
.
On the first one, just draw a rectangle
. On the other draw a rectangle
with the word hello
inside it.
Now convince yourself that your view only has two states, namely hasFadedIn
is true
or false
. Interestingly, when your variable changes from true
to false
, SwiftUI immediately redraws the view. It's immediate.
What you perceive as animation is SwiftUI handing the before and after views to an animation routine that interpolates the inbetween frames over the duration that you specify. While the animation routine shows the frames onscreen, behind the scene the new view has already been drawn. Depending upon the device, the animation routine might draw 15 inbetween frames, or 15 hundred inbetween frames. You don't know, and you don't care. SwiftUI will calculate an appropriate number of inbetween frames it needs for smooth animation.
So you wonder why the .animation()
modifier doesn't affect everything in the ZStack
? It does! It's just that the rectangle
is always present. When the OpeningView
struct is created, the rectangle
is present from the start.