Animating a fan image
There are actually two components left to create, the fan and the motor. However, we will add the motor when we start piecing all of the files together inside ContentView
. For that reason, let’s focus on creating the fan now.
Like always, create a new file, select the SwiftUI View template, and call it FanView
. In this file, we only need two variables – one for the state of the animation and one to hold the degrees of rotation for the fan; here they are:
@State private var rotateFan = false var degrees: Double = 360 * 4
This file is quite short, so just add the following code to complete it:
var body: some View { ZStack { Image("fan").resizable().aspectRatio(contentMode: .fit).frame(width: 200) ...