tfmart/lottieui
LottieUI allows you to use delightful [Lottie](https://airbnb.design/lottie/) animations with SwiftUI without having to give up on the familiar declarative API or the powerful customization settings from the animation framework
π° Remote animations
For remote animations, LottieUI provides AsyncLottieView, which attemps to download an animation from a remote URL and present it if successful. You can also provide views to be displayed while the donwload is in progress or if the download fails:
let url = URL(string: "https://assets3.lottiefiles.com/packages/lf20_hbdelex6.json")!
AsyncLottieView(url: url) { phase in
switch phase {
case .loading:
ProgressView()
case .error:
ErrorView
case .success(let lottieView):
lottieView
}
}π Features
LottieView allows you to take control of your animations with a set of modifiers that can be applied to a LottieView:
β― Playback
By default, your animation will start playing automatically. To control whether the animation should be playing, use the .play(_:) modifier:
struct ContentView: View {
@State var isPlaying: Bool = true
var body: some View {
LottieView("MyAnimation")
.play(isPlaying)
}
}[Playback modifier demo]
π Loop Mode
To setup the Loop Mode for your animation, use .loopMode(_:)
struct ContentView: View {
var body: some View {
LottieView("MyAnimation")
.loopMode(.loop)
}
}[Speed modifier demo]
πΌ Current Frame and Progress
To observe the current frame being displayed in the animation and perform an action based on it, use .onFrame(_:)
Warning To make use of the Frame/Progress observers, the animation must be using the
.mainThreadrendering engine. This can be set by using the.renderingEngine(_:)method
struct ContentView: View {
var body: some View {
LottieView("MyAnimation")
.renderingEngine(.mainThread)
.onFrame { _ in
// Perform action based on current frame
}
}
}To observe the progress instead, use .onProgress(_:):
struct ContentView: View {
var body: some View {
LottieView("MyAnimation")
.renderingEngine(.mainThread)
.onProgress { _ in
// Perform action based on current progress
}
}
}[Progress Observer modifier demo]
π Speed
To set the speed of an animation, use .speed(_:):
struct ContentView: View {
var body: some View {
LottieView("MyAnimation")
.speed(2.0)
}
}[Speed modifier demo]
Rendering Engine
LottieUI also supports the new RenderingEngine introduced in Lottie 3.4.0, which can greatly reduce CPU usage when displaying compatible animations
By default, LottieUI uses the .automatic, which will automatically apply the new rendering engine if an animation is compatible, but you can override it with the .renderingEngine(_:) modifier:
LottieView("MyAnimation")
.renderingEngine(.coreAnimation)[Rendering Engine modifier demo]
There are many other options available such as:
- Limit the framerate of an animation with
.play(fromFrame:to:) - Define the background behavior of the animation with
.backgroundBehavior(_:) - Set the value provider for a specific keypath of the animation with
.valueProvider(_: keypath:)
For more information check the included documentation in each public component and modifier
π Installation
Swift Package Manager
In your project's Package.swift file, add LottieUI as a dependency:
.package(name: "LottieUI", url: "https://github.com/tfmart/LottieUI", from: "1.0.0")Package Metadata
Repository: tfmart/lottieui
Default branch: main
README: README.md