Francis F
Francis F

Reputation: 3285

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?

Upvotes: 3

Views: 7582

Answers (4)

Wimukthi Rajapaksha
Wimukthi Rajapaksha

Reputation: 1019

let animationSubView = AnimationView(name: "jsonAnimationFileName")
animationSubView.frame = self.checkMarkAnimationView.bounds
animationSubView.contentMode = .scaleAspectFit

self.checkMarkAnimationView.addSubview(animationSubView)
animationSubView.play()

Add this to viewDidLoad()

Upvotes: 0

Matheus Lima
Matheus Lima

Reputation: 178

    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()

Upvotes: 3

Sachin Kishore
Sachin Kishore

Reputation: 324

  let animationView = AnimationView()
 let path = Bundle.main.path(forResource: "loader",
                                     ofType: "json") ?? ""
  animationView.animation = Animation.filepath(path)
        animationView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
        animationView.center = self.view.center
        animationView.loopMode = .loop
        self.view.addSubview(animationView)
        animationView.play()

Upvotes: 0

Mike H
Mike H

Reputation: 507

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)

Upvotes: 3

Related Questions