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 repository at https://github.com/PacktPublishing/SwiftUI-Cookbook-3rd-Edition/tree/main/Resources/Chapter09/recipe9, 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
.
Now, create a new SwiftUI project in Xcode called CustomViewTransition
, and copy the image1.jpg
and image2.jpg
images to the Assets
catalog...