Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Lottie Animation in iOS

I'm new to Lottie library and trying to integrate a simple animation. I have my animated json at folder Resources->Json->LottieAnimations->checkMark.json

I have installed Lottie via pod install and its current version is 3.1.0. In my storyboard I have added a view and gave the class name as AnimationView and in the interface builder gave the Animation Name as "checkMark".

This is the outlet

@IBOutlet weak var checkMarkAnimationView: AnimationView!

And on viewDidLoad I just call

checkMarkAnimationView.play()

But nothing happens here. Am I missing something?

like image 582
Francis F Avatar asked Dec 06 '25 14:12

Francis F


2 Answers

You need to do something like this (if in the code):

 let view = AnimationView()
    let path = Bundle.main.path(forResource: "checkMark",
                                ofType: "json") ?? ""
    view.animation = Animation.filepath(path)
    self.view.addSubview(view)
    view.play()

if doesn't work, please check animation name or JSON file

Updated

also check is JSON file added in Bundle resources (Build phases -> Copy Bundle Resources)

like image 146
Mike H Avatar answered Dec 09 '25 04:12

Mike H


    let animationView = AnimationView()
    let animation = Animation.named("<place-your-json-filename>", animationCache: LRUAnimationCache.sharedCache)
    self.view.addSubview(animationView)
    animationView.frame = self.view.bounds
    animationView.animation = animation
    animationView.loopMode = .loop
    animationView.play()
like image 45
Matheus Lima Avatar answered Dec 09 '25 02:12

Matheus Lima