Posted on 13 Dec 2023 . 2 min read
Lottie is a fantastic tool for incorporating high-quality animations into your SwiftUI projects. There are several ways to add Lottie to project. But in this tutorial we will use Swift Package Manager(SPM). To use Lottie animations in SwiftUI, you can follow these steps:
In this article:
First, you’ll need to add the Lottie package to your project. You can do this using Swift Package Manager. In Xcode, go to File -> Swift Packages -> Add Package Dependency, then enter the Lottie GitHub repository URL: https://github.com/airbnb/lottie-ios.git
Once you’ve added the Lottie package, import it into your SwiftUI file where you want to use the animation:
import Lottie
Create a SwiftUI view that incorporates the Lottie animation. For example:
struct LottieAnimationView: UIViewRepresentable {
var filename: String
var loopMode: LottieLoopMode = .loop
func makeUIView(context: Context) -> some UIView {
let view = UIView()
let animationView = AnimationView()
animationView.animation = Animation.named(filename)
animationView.contentMode = .scaleAspectFit
animationView.loopMode = loopMode
animationView.play()
animationView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(animationView)
NSLayoutConstraint.activate([
animationView.widthAnchor.constraint(equalTo: view.widthAnchor),
animationView.heightAnchor.constraint(equalTo: view.heightAnchor),
animationView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
animationView.centerYAnchor.constraint(equalTo: view.centerYAnchor)
])
return view
}
func updateUIView(_ uiView: UIViewType, context: Context) {}
}
Now, you can use the LottieAnimationView within your SwiftUI code:
struct ContentView: View {
var body: some View {
VStack {
LottieAnimationView(filename: "animation-file-name")
.frame(width: 300, height: 300)
}
}
}
Replace "animation-file-name" with the actual name of the Lottie animation file you have in your project.
You can further customize the LottieAnimationView by adding parameters or methods to control the animation's behavior, like adjusting its size, playback speed, or defining different animations based on conditions.
Ensure the Lottie animation file is added to your Xcode project and is included in the target membership to make it accessible in your code.
This should get you started with integrating Lottie animations into your SwiftUI project. Feel free to tweak and customize the implementation to suit your specific animation needs!
Don’t hesitate to contact me if you have any questions or queries. Follow me on twitter @gurjitpt for any updates.
Thanks!
Written By
Gurjit Singh
I’m Computer Science graduate and an iOS Engineer who writes about Swift and iOS development. Follow me for more updates:
Discover articles by topics
SwiftUI Class Struct Networking XCode NSCache Enum Optionals Property Observers Closures Guard Reviews StoreKit App Store Algorithms Testing Operators Protocol Extensions Weak Unowned SwiftData WWDC23 GCD API Admob SwiftLint Lottie Foreach Objective-C UIKit NavigationSplitView
In any programming language, working with strings is essential, and Swift is no different.Whether you are building iOS apps......
2024-10-17 . 3 min read String Concatenation
With the introduction of SwiftUI, Apple has provided developers with a modern way to build user interfaces across all Apple platforms....
2024-07-09 . 3 min read UIHostingController
In the realm of software development, memory management plays a crucial role in ensuring the efficient allocation and deallocation of memory...
2024-01-28 . 4 min read Swift Autorelease
Swift enums provide a powerful way to model a set of related values. Enums can be equipped with associated values, allowing them to represen...
2024-01-24 . 3 min read Swift Enums
Use a DatePicker when creating a view that enables users to choose both a calendar date and, if needed, a specific time.In SwiftUI, you can ...
2024-01-16 . 2 min read SwiftUI DatePicker
SwiftLint is a tool that ensures Swift code adheres to defined style guidelines. It automates code review by identifying and suggesting impr...
2023-12-29 . 4 min read Swift SwiftLint