lottie-ios icon indicating copy to clipboard operation
lottie-ios copied to clipboard

Looping doesn't work with dotLottie in UIKit

Open SaladDays831 opened this issue 1 year ago • 3 comments

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

SaladDays831 avatar Jan 03 '24 21:01 SaladDays831

Can you share the animation file itself here? I can't open the lottiefile.com link: image

calda avatar Jan 04 '24 14:01 calda

Oh, sorry @calda, thought it was a public animation

LottieLego.zip

SaladDays831 avatar Jan 04 '24 15:01 SaladDays831

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 the animationLoaded block, the .loop mode is ignored.
  • As indicated by @SaladDays831, If I set the loopMode = .loop inside the animationLoaded, right before the play(), it works. The animation loops.

mime29 avatar Jan 05 '24 06:01 mime29

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.

calda avatar Jan 08 '24 18:01 calda

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: #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.

mime29 avatar Jan 09 '24 01:01 mime29