I'm attempting to construct an animation using SwiftUI.
Start: [ A ][ B ][ D ]
End: [ A ][ B ][ C ][ D ]
The key elements of the animation are:
- C should appear to slide out from underneath B (not expand from zero width)
- The widths of all views are defined by subviews, and are not known
- The widths of all subviews should not change during or after the animation (so, total view width is larger when in the end state)
I'm having a very difficult time satisfying all of these requirements with SwiftUI, but have been able to achieve similar affects with auto-layout in the past.
My first attempt was a transition using an HStack
with layoutPriorities. This didn't really come close, because it affects the width of C during the animation.
My second attempt was to keep the HStack
, but use a transition with asymmetrical move animations. This came really close, but the movement of B and C during the animation does not give the effect that C was directly underneath B.
My latest attempt was to scrap relying on an HStack
for the two animating views, and use a ZStack
instead. With this setup, I can get my animation perfect by using a combination of offset
and padding
. However, I can only get it right if I make the frame sizes of B and C known values.
Does anyone have any ideas on how to achieve this effect without requiring fixed frame sizes for B and C?