0

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)
    }
    ...
}
Mout Pessemier
  • 1,665
  • 3
  • 19
  • 33

2 Answers2

2

Above

import lottie

and give the view a frame

animationView.animation = animation
animationView.frame = ///
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

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!

Keshu R.
  • 5,045
  • 1
  • 18
  • 38