Lottie iOS is a cross-platform animation library for iOS, macOS, tvOS, and visionOS that natively renders vector-based animations and art in realtime with minimal code. It supports loading animations from JSON files, .lottie packages, and provides comprehensive playback controls, dynamic property modification, and SwiftUI integration.
pod 'lottie-ios' or Swift Package Manager: https://github.com/airbnb/lottie-ios.gitimport Lottieimport Lottie
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Load and display animation
let animationView = LottieAnimationView(name: "animation")
animationView.frame = view.bounds
animationView.contentMode = .scaleAspectFit
animationView.loopMode = .loop
view.addSubview(animationView)
// Start playing
animationView.play { finished in
print("Animation completed: \(finished)")
}
}
}SwiftUI usage:
import SwiftUI
import Lottie
struct ContentView: View {
var body: some View {
LottieView(animation: .named("animation"))
.playing(loopMode: .loop)
.resizable()
.frame(width: 200, height: 200)
}
}Lottie iOS is built around several key components:
LottieAnimation represents the animation data loaded from JSON or .lottie filesLottieAnimationView (UIKit), LottieAnimationLayer (Core Animation), and LottieView (SwiftUI) for displayThe foundational animation class containing all animation data and helper methods.
public final class LottieAnimation {
public let startFrame: AnimationFrameTime
public let endFrame: AnimationFrameTime
public let framerate: Double
public let duration: TimeInterval
public let bounds: CGRect
public let size: CGSize
public let markerNames: [String]
public static func named(
_ name: String,
bundle: Bundle = Bundle.main,
subdirectory: String? = nil,
animationCache: AnimationCacheProvider? = nil
) -> LottieAnimation?
public static func filepath(
_ filepath: String,
animationCache: AnimationCacheProvider? = nil
) -> LottieAnimation?
public static func from(
data: Data,
strategy: DecodingStrategy = .dictionaryBased
) throws -> LottieAnimation
}Core view components for displaying Lottie animations across different UI frameworks.
// UIKit Animation View
open class LottieAnimationView: UIView {
public var animation: LottieAnimation?
public var loopMode: LottieLoopMode
public var animationSpeed: CGFloat
public var currentProgress: AnimationProgressTime
public var isAnimationPlaying: Bool
public func play(completion: LottieCompletionBlock? = nil)
public func play(
fromProgress: AnimationProgressTime,
toProgress: AnimationProgressTime,
loopMode: LottieLoopMode? = nil,
completion: LottieCompletionBlock? = nil
)
public func stop()
public func pause()
}
// SwiftUI Animation View
public struct LottieView: View {
public init(animation: LottieAnimation?)
public func playing(_ playbackMode: LottiePlaybackMode) -> LottieView
public func looping() -> LottieView
public func resizable() -> LottieView
}Comprehensive playback modes and control systems for fine-grained animation management.
public enum LottiePlaybackMode {
case paused(at: PausedState)
case playing(_ mode: PlaybackMode)
}
public enum LottieLoopMode {
case playOnce
case loop
case autoReverse
case `repeat`(Float)
case repeatBackwards(Float)
}
public enum PausedState {
case currentFrame
case progress(AnimationProgressTime)
case frame(AnimationFrameTime)
case time(TimeInterval)
case marker(String, position: MarkerPosition = .start)
}Runtime modification of animation properties through keypaths and value providers.
public struct AnimationKeypath: Hashable {
public let keys: [String]
public let string: String
public init(keypath: String)
public init(keys: [String])
}
public protocol AnyValueProvider {
var valueType: Any.Type { get }
var typeErasedStorage: AnyValueProviderStorage { get }
func hasUpdate(frame: AnimationFrameTime) -> Bool
}
public final class ColorValueProvider: AnyValueProvider {
public init(block: @escaping (AnimationFrameTime) -> LottieColor)
public init(_ color: LottieColor)
public init(_ keyframes: [Keyframe<LottieColor>])
}Global configuration options for rendering engines, color spaces, and performance tuning.
public struct LottieConfiguration {
public static var shared: LottieConfiguration
public var renderingEngine: RenderingEngineOption
public var decodingStrategy: DecodingStrategy
public var colorSpace: CGColorSpace
public var reducedMotionOption: ReducedMotionOption
}
public enum RenderingEngineOption {
case automatic
case specific(RenderingEngine)
case mainThread
case coreAnimation
}
public enum RenderingEngine {
case mainThread
case coreAnimation
}Support for .lottie compressed animation files containing multiple animations, images, and configuration data.
public final class DotLottieFile {
public struct Animation {
public let animation: LottieAnimation
public let configuration: DotLottieConfiguration
}
public private(set) var animations: [Animation]
public static func named(
_ name: String,
bundle: Bundle = Bundle.main,
subdirectory: String? = nil
) -> DotLottieFile?
public func animation(for id: String? = nil) -> Animation?
public func animation(at index: Int) -> Animation?
}
public struct DotLottieConfiguration {
public let id: String
public let loop: Bool
public let themeColor: String?
public let speed: Double
}SwiftUI and UIKit animated controls using Lottie animations for rich interactive experiences.
// SwiftUI Controls
public struct LottieButton: View {
public init(animation: LottieAnimation?, action: @escaping () -> Void)
public func configure(_ configure: @escaping (AnimatedButton) -> Void) -> Self
}
public struct LottieSwitch: View {
public init(animation: LottieAnimation?)
public func configure(_ configure: @escaping (AnimatedSwitch) -> Void) -> Self
}
// UIKit Controls
open class AnimatedButton: UIControl {
public var animationView: LottieAnimationView
public var performAction: (() -> Void)?
}
open class AnimatedSwitch: UIControl {
public var animationView: LottieAnimationView
public var isOn: Bool
public func setIsOn(_ isOn: Bool, animated: Bool)
}Caching system for loaded animations to improve performance and reduce memory usage.
public protocol AnimationCacheProvider {
func animation(forKey: String) -> LottieAnimation?
func setAnimation(_ animation: LottieAnimation, forKey: String)
func clearCache()
}
public class LottieAnimationCache: AnimationCacheProvider {
public static let shared: LottieAnimationCache
public var cacheSize: Int
public var countLimit: Int
}Extensible provider system for supplying external assets like images, fonts, and text.
public protocol AnimationImageProvider {
func imageForAsset(asset: ImageAsset) -> CGImage?
func contentsGravity(for asset: ImageAsset) -> CALayerContentsGravity
var cacheEligible: Bool { get }
}
public protocol AnimationFontProvider {
func fontFor(family: String, size: CGFloat) -> CTFont?
}
public protocol AnimationKeypathTextProvider {
func text(for keypath: AnimationKeypath, sourceText: String?) -> String?
}public typealias AnimationProgressTime = CGFloat
public typealias AnimationFrameTime = CGFloat
public typealias LottieCompletionBlock = (Bool) -> Void
public struct LottieColor {
public let r: Double
public let g: Double
public let b: Double
public let a: Double
public init(r: Double, g: Double, b: Double, a: Double, denominator: ColorFormatDenominator = .One)
}
public enum ColorFormatDenominator {
case One
case OneHundred
case TwoFiftyFive
}
public struct LottieVector1D {
public let value: Double
}
public struct LottieVector3D {
public let x: Double
public let y: Double
public let z: Double
}public enum DecodingStrategy: Hashable {
case legacyCodable
case dictionaryBased
}
public enum CoordinateSpace: Int, Codable {
case type2d
case type3d
}
public enum LottieBackgroundBehavior {
case stop
case pause
case pauseAndRestore
case forceFinish
case continuePlaying
public static func `default`(for renderingEngine: RenderingEngine) -> LottieBackgroundBehavior
}
public enum ReducedMotionOption {
case disableAnimation
case continuePlaying
}public final class LottieLogger {
public static var shared: LottieLogger
public typealias Assert = (_ condition: @autoclosure () -> Bool, _ message: @autoclosure () -> String, _ file: StaticString, _ line: UInt) -> Void
public typealias AssertionFailure = (_ message: @autoclosure () -> String, _ file: StaticString, _ line: UInt) -> Void
public typealias Warn = (_ message: @autoclosure () -> String, _ file: StaticString, _ line: UInt) -> Void
public typealias Info = (_ message: @autoclosure () -> String) -> Void
public init(assert: @escaping Assert, assertionFailure: @escaping AssertionFailure, warn: @escaping Warn, info: @escaping Info)
}public struct ImageAsset {
public let id: String
public let name: String
public let directory: String
public let width: Double
public let height: Double
}
public struct FontAsset {
public let name: String
public let family: String
public let style: String
public let ascent: Double
}