Creating custom view transitions
SwiftUI has a nice feature that gives us the possibility to add an animation when a view appears or disappears. It is called a transition, and it can be animated with the usual degree of customization.
In this recipe, we'll see how to create custom appearing and disappearing transitions by combining different transitions.
Getting ready
This recipe uses two images courtesy of Erika Wittlieb from Pixabay (https://pixabay.com/users/erikawittlieb-427626/).
You can find the images in the GitHub repo at the following link, https://github.com/PacktPublishing/SwiftUI-Cookbook-2nd-Edition/tree/main/Resources/Chapter08/recipe7, but you can also use your own images for this recipe. If you don't want to use your own images, you can replace them with two symbols from SF Symbols, such as "gamecontroller
" or "car
."
Then, create a new SwiftUI project in Xcode called CustomViewTransition
, and copy the ch8-r7-i1.jpg
and ch8...