Repeating Animation in SwiftUI

Kelvin Tan
Swiftly Engineered iOS
4 min readNov 5, 2020

--

Phone over a blue backdrop
Photo by Al Hakiim on Unsplash.

Build beautiful animations that repeat in your apps

With so much more support provided by Apple, animation in SwiftUI is a lot easier to achieve than in UIKit. I wonder if this will be a good or bad thing. Regardless, we need to make use of what is provided, right?

In this tutorial, you’ll learn how to perform a repeat animation in SwiftUI.

A repeat animation is basically an animation that repeats over and over again. Pretty self-explanatory. You can imagine it as having the same behaviour as a GIF that keeps repeating.

Prerequisites

To follow along with this tutorial, you’ll need some basic knowledge in:

  • Swift
  • At least Xcode 11

Setting Up the View

You will be using RoundedRectangle here. You may choose any shape you like, but for now, we will be using RoundedRectangle.

To get a head start, this is basically what you will need: a State to know if it is animating or not.

@State private var isAnimating = false

And a RoundedRectangle embedded inside a VStack. The animation used here will be scaling the RoundedRectangle:

--

--

Kelvin Tan
Swiftly Engineered iOS

Father, husband, software engineer. Building software and building a family, one line of code and one moment at a time. 🚀💻💙 http://ko-fi.com/kelvintanzy