0

I updated to the latest version (3.0) of Lottie and noticed various changes that forced me to alter my code.

Previously, I wanted to be able position animations via storyboard, and to do so, I used a LOTAnimatedControl and then in code accessed the animationView of the control to set the animation via LOTAnimatedControl.animationView.setAnimation(named: "x").

Now it appears this is not possible at all.

What I thought I could do then is.. 1. Set a uiview outlet in storyboard. 2. Subview an AnimationView initialized in code. 3. Setting the animation via AnimationView.animation = Animation.named("name")

I set it all up, but I cannot see the animation when I run it. In debugging I see that Animation.named("x") prints out to nil, but not sure why. I removed the .json extension in the name and the .json file is exactly where it was before when it was working.

Any ideas would be welcomed. Below is my setup.

@IBOutlet weak var activityIndicatorView: UIView!

let activityIndicator = AnimationView()

activityIndicatorView.addSubview(activityIndicator)
    NSLayoutConstraint.activate([
        activityIndicator.centerXAnchor.constraint(equalTo: activityIndicatorView.centerXAnchor),
        activityIndicator.centerYAnchor.constraint(equalTo: activityIndicatorView.centerYAnchor),
        activityIndicator.widthAnchor.constraint(equalToConstant: activityIndicatorView.frame.width),
        activityIndicator.heightAnchor.constraint(equalToConstant: activityIndicatorView.frame.height)
        ])
let animation = Animation.named("name")
    activityIndicator.animation = animation
    activityIndicator.isHidden = false
    activityIndicator.loopMode = .loop
    activityIndicator.play()
Chris
  • 431
  • 8
  • 33

2 Answers2

1

You need

activityIndicator.translatesAutoresizingMaskIntoConstraints = false

to correctly add programmatic constraints

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
1

Still not sure why I was getting nil as I followed the quickstart lottie ios documentation, but my workaround was on initialization of AnimationView, I would just bundle it together with the actual animation on initialization - AnimationView(name: "name").

Chris
  • 431
  • 8
  • 33