Learn iOS/SwiftUI Spring Animations: Beyond the Basics
Let's unlock the secrets of iOS spring animations in SwiftUI. Learn all the SwiftUI spring animation types, understand their parameters, and discover how to create organic and buttery animations
Resources
Enter the Physics of the Spring
You do not need to be in a Physics motion class to animate with the spring
Have 23 minutes? Learn all the spring concepts in this video.?
.spring()
Let's crawl before we start walking. You will begin with a spring that has no parameters .spring(). This spring applies a gentle and sensible spring feel to the object you want to animate. Here is the Swift file.
import SwiftU
struct Spring: View {
? ??
? ? @State private var isRotating = false
? ? @State private var isHidden = false
? ??
? ? var body: some View {
? ? ? ? VStack(spacing: 14){
? ? ? ? ? ??
? ? ? ? ? ? Rectangle() // top
? ? ? ? ? ? ? ? .frame(width: 64, height: 10)
? ? ? ? ? ? ? ? .cornerRadius(4)
? ? ? ? ? ? ? ? .rotationEffect(.degrees(isRotating ? 48 : 0), anchor: .leading)
? ? ? ? ? ??
? ? ? ? ? ? Rectangle() // middle
? ? ? ? ? ? ? ? .frame(width: 64, height: 10)
? ? ? ? ? ? ? ? .cornerRadius(4)
? ? ? ? ? ? ? ? .scaleEffect(isHidden ? 0 : 1, anchor: isHidden ? .trailing: .leading)
? ? ? ? ? ? ? ? .opacity(isHidden ? 0 : 1)
? ? ? ? ? ??
? ? ? ? ? ? Rectangle() // bottom
? ? ? ? ? ? ? ? .frame(width: 64, height: 10)
? ? ? ? ? ? ? ? .cornerRadius(4)
? ? ? ? ? ? ? ? .rotationEffect(.degrees(isRotating ? -48 : 0), anchor: .leading)
? ? ? ? }
? ? ? ? .onTapGesture {
? ? ? ? ? ? withAnimation(.spring()){
? ? ? ? ? ? ? ? isRotating.toggle()
? ? ? ? ? ? ? ? isHidden.toggle()
? ? ? ? ? ? }
? ? ? ? }
? ? ? ??
? ? }
}
struct Spring_Previews: PreviewProvider {
? ? static var previews: some View {
? ? ? ? Spring()
? ? ? ? ? ? .preferredColorScheme(.dark)
? ? }
}I
As you see from the above, this spring gives the animation a gentle feel and that may be all you need. Nice huh?
.interactiveSpring()
Your animation does not need to feel gentle all the time. You can make it snappy too. This type creates a spring animation with high stiffness and low response. It results in a snappy spring animation. Swift file.
.interpolatingSpring(stiffness, damping)
This type allows you to create a spring animation
.interpolatingSpring(mass, stiffness, damping, initialVelocity)
This allows you to create a spring animation that is based on mass, stiffness, damping, and initial velocity.?Default values: .interpolatingSpring (mass: Double = 1.0, stiffness: Double, damping: Double, initialVelocity: Double = 0.0).
领英推荐
.spring(response, dampingFraction, blendDuration)
This type allows you to create a spring animation based on the response, damping fraction, and blend duration.?Default values: .spring (response: Double = 0.55, dampingFraction: Double = 0.825, blendDuration: Double = 0).?A higher response value will slow down the animation. A lower response speeds it up.
Additionally, you can dampen the spring in the following ways.
.interactiveSpring(response, dampingFraction, blendDuration
This type allows you to create a spring animation based on the response, damping fraction, and blend duration.?Default values: interactiveSpring (response: Double = 0.15, dampingFraction: Double = 0.86, blendDuration: Double = 0.25).?You can use this spring as an alternative to .spring(response, dampingFraction, blendDuration). Always start with the default values above. To check the default values in XCode, Control-click the spring modifier and select “Show Quick Help”. The thumbs-down animation below uses this spring. The damping fraction is inversely proportional to the springiness of the animation. Reducing the default damping fraction will make it bouncier.?Swift file.
SwiftUI Spring Animation Properties and Effects
Damping Fraction: High, medium, low, and no bounce
Stiffness Bounce: High, low, medium, and very low stiffness
Varying Stiffness and Damping: Stiff, gentle, wobble, and no wobble
What Next?
This article serves as your reference and complete guide for SwiftUI Spring Animations. It demonstrates use cases for the various types of spring animations and parameters. No more guessing the values of the parameters for spring animations you create for your next iOS app. Clone the GitHub repo and use it as your spring animations cheat sheet. It is packed with several other spring animation examples. Enjoy.
Check out the following to learn more.
MSc HCI | Developer Advocate
1 周I will update it soon
Mobile Developer || iOS || iPhone || Objective C || Swift || SwiftUI Actively looking for iOS Developer role.
1 周Great article!
iOS Developer | Swift | Objective-C | Kotlin Multiplatform | Banking & Insurance | Medium Writer
1 年I really enjoy your posts
quite deep!
iOS Developer | Swift | Mobile | UIKit | SwiftUI
1 年Great article!