Mout Pessemier
Mout Pessemier

Reputation: 1995

lottie animations is nil in ios

I'm trying to use Lottie animations inside my ios app but I can't get them to work. I've seen people use LOTAnimationViews but I can't import or use this class. I've added an Animation folder with my animations in.

ViewController:

class CardStackViewController: UIViewController, CLLocationManagerDelegate, NetworkManagerDelegate {
    ...
    private let animationView = AnimationView()
    ...

    ...
    func didFail(with error: Error) {
        if let animation = Animation.named("error", subdirectory: "Animations") {
            animationView.animation = animation
            animationView.loopMode = .loop
            view.addSubview(animationView)
            animationView.play()
        }
        print("---DIDFAIL WITH ERROR---", error.localizedDescription, error)
    }
    ...
}

Upvotes: 0

Views: 1211

Answers (2)

Keshu R.
Keshu R.

Reputation: 5223

If are want to achieve it using a storyboard, follow these steps:

  1. Drag and drop a view in your View controller.
  2. Change the class of the View to AnimationView and module to Lottie as shown below.

Like this

  1. Now in your ViewController class, import Lottie and create an outlet like this:

    import Lottie
    
    class YourVC : UIViewController { 
    @IBOutlet weak var myAnimationView : AnimationView!
    }
    
  2. Now in your ViewDidLoad function setup your animation like this:

    let animationToShow = Animation.named("yourAnimationFileNameWithoutJSONextension")
    myAnimationView.animation = animationToShow
    myAnimationView.animationSpeed = 1.0
    myAnimationView.loopMode = .loop
    myAnimationView.contentMode = .scaleAspectFit
    myAnimationView.play()
    

Done!

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100533

Above

import lottie

and give the view a frame

animationView.animation = animation
animationView.frame = ///

Upvotes: 2

Related Questions