repeatCount(_:autoreverses:)
Repeats the animation for a specific number of times.
Declaration
func repeatCount(_ repeatCount: Int, autoreverses: Bool = true) -> AnimationParameters
- repeatCount:
The number of times that the animation repeats. Each repeated sequence starts at the beginning when
autoreverseisfalse. - autoreverses:
A Boolean value that indicates whether the animation sequence plays in reverse after playing forward. Autoreverse counts towards the
repeatCount. For instance, arepeatCountof one plays the animation forward once, but it doesn’t play in reverse even ifautoreverseistrue. WhenautoreverseistrueandrepeatCountis2, the animation moves forward, then reverses, then stops.
Return Value
An animation that repeats for specific number of times.
Discussion
Use this method to repeat the animation a specific number of times. For example, in the following code, the animation moves a truck from one edge of the view to the other edge. It repeats this animation three times.
struct ContentView: View {
@State private var driveForward = true
private var driveAnimation: Animation {
.easeInOut
.repeatCount(3, autoreverses: true)
.speed(0.5)
}
var body: some View {
VStack(alignment: driveForward ? .leading : .trailing, spacing: 40) {
Image(systemName: "box.truck")
.font(.system(size: 48))
.animation(driveAnimation, value: driveForward)
HStack {
Spacer()
Button("Animate") {
driveForward.toggle()
}
Spacer()
}
}
}
}The first time the animation runs, the truck moves from the leading edge to the trailing edge of the view. The second time the animation runs, the truck moves from the trailing edge to the leading edge because autoreverse is true. If autoreverse were false, the truck would jump back to leading edge before moving to the trailing edge. The third time the animation runs, the truck moves from the leading to the trailing edge of the view.