lottie-ios
lottie-ios copied to clipboard
Looping doesn't work with dotLottie in UIKit
loopMode = .loop
is ignored when using LottieAnimationView(dotLottieName: ...)
Also tried the (unnecessary more complex?) approach from the video - same issue. Even in the video (which is on the official LottieFiles channel btw 😁) the looping clearly doesn't work, although the author uses loopMode = .loop
The animation does loop though when calling animationView.loopMode = .loop
just before calling animationView.play()
Example:
import UIKit
import Lottie
final class LottieTestViewController: UIViewController {
private let animationView = LottieAnimationView(dotLottieName: "LottieLego")
private let button = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
animationView.loopMode = .loop
button.backgroundColor = .brown
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
view.addSubview(animationView)
animationView.translatesAutoresizingMaskIntoConstraints = false
animationView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
animationView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
animationView.widthAnchor.constraint(equalToConstant: 100).isActive = true
animationView.heightAnchor.constraint(equalToConstant: 100).isActive = true
view.addSubview(button)
button.translatesAutoresizingMaskIntoConstraints = false
button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -20).isActive = true
button.widthAnchor.constraint(equalToConstant: 200).isActive = true
button.heightAnchor.constraint(equalToConstant: 80).isActive = true
}
@objc private func buttonAction() {
animationView.play()
}
}
Lottie 4.3.4
Expected Behavior
The animation should loop and play until stopped
Actual Behavior
The animation only plays once
Animation JSON
https://app.lottiefiles.com/animation/ee0a6947-bd84-4065-9110-66d19d31e37a
Can you share the animation file itself here? I can't open the lottiefile.com link:
I identified the same problem here as well. With dotLottie files only. JSON files are working fine. Even If I set the loopMode on the view right after building it I can observe:
- If I set the
loopMode
before setting up theanimationLoaded
block, the.loop
mode is ignored. - As indicated by @SaladDays831, If I set the
loopMode = .loop
inside theanimationLoaded
, right before the play(), it works. The animation loops.
I believe this is the expected behavior of a dotLottie animation, since the json manifest includes configuration on the loop mode to use, which we apply to the LottieAnimationView
when loading the animation.
@mime29's solution is a good suggestion -- if you modify the loopMode
in animationLoaded
, that will override whatever was specified by the DotLottie animation.
We've improved this in the SwiftUI LottieView by not applying the dotLottie loop mode by default, and instead making that behavior opt-in: https://github.com/airbnb/lottie-ios/pull/2277.
I believe this is the expected behavior of a dotLottie animation, since the json manifest includes configuration on the loop mode to use, which we apply to the
LottieAnimationView
when loading the animation.@mime29's solution is a good suggestion -- if you modify the
loopMode
inanimationLoaded
, that will override whatever was specified by the DotLottie animation.We've improved this in the SwiftUI LottieView by not applying the dotLottie loop mode by default, and instead making that behavior opt-in: #2277.
@calda , I guess you meant: "since the dotLottie manifest includes configuration on the loop mode to use" ? Is there a link to this spec? I could not find it but I'd like to forward this info to designers so that they understand better the control they have over the animation.