9

I am loading the animation typing indicator from the json file using lottie framework. I want to change the animation of the indicator color. I don't want to change the colour from the json file. Want to change the colour for animation view programmatically.

Eg : (. . . .) -> Typing indicator (Want to change the dot colour)

  private let animationView = LOTAnimationView(name: 
  Constants.ImageAssets.typingIndicatorIcon, bundle:Bundle(identifier: Constants.GenericKeys.bundleIdentifier)!)

    private func loadTypingIndicator() {
    animationView.loopAnimation = true
    animationView.translatesAutoresizingMaskIntoConstraints = true
    // *** It's not working 
    animationView.setValue(UIColor.green, forKeypath: "boule.Ellipse 1.Fill 1.Color", atFrame: 0)
    animationView.play()
}
TylerH
  • 20,799
  • 66
  • 75
  • 101
Devan
  • 147
  • 1
  • 1
  • 8

5 Answers5

21

Maybe this helps:

let keypath = AnimationKeypath(keys: ["**", "Fill", "**", "Color"])
let colorProvider = ColorValueProvider(UIColor.green.lottieColorValue)
animationView.setValueProvider(colorProvider, keypath: keypath)
TylerH
  • 20,799
  • 66
  • 75
  • 101
G. Meira
  • 310
  • 1
  • 6
  • 13
    Works, but check your animation keypaths with AnimationView.logHierarchyKeypaths(), because not always the keypaths will be "Fill". – Pelanes Dec 02 '19 at 13:51
  • 4
    In my case, I printed the logHierarchyKeypaths() and used the first word that showed up in the list to replace the "Fill" on the Animationkeypath. And it worked. – Roney Sampaio Dec 04 '19 at 05:42
  • 6
    In my case I had to replace Fill with Fill 1. It is worth to log the keys and see which one it's being used for the color. Thanks folks! – Leonel Menezes Morato Lima Dec 30 '19 at 18:17
  • Android has a simpler way to get color property, doesn't iOS have a simpler way rather than checking the keypaths and checking which one overrides the color we need? – Joshua Vidamo Jan 20 '20 at 13:32
6

Bumped into this question... in case anyone wanted to know more, here's a tutorial about using value provider to change the Lottie animation color. https://swiftsenpai.com/development/lottie-value-providers/

Lee Kah Seng
  • 183
  • 1
  • 6
  • 6
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Rajan May 15 '20 at 04:34
1
  • Because the animations can be complex and have multiple colours. The 'Layers' can be edited using the LottieEditor by clicking on "Edit Layer Colours". Then save the .JSON to a file and add to the XCode project.
  • Then in code, what I did is just refer to the .JSON file with the colour desired depending on the UI color scheme. So I suffix the file with its unique color. ie. download_black.json, download_blue.json.
0

As option - use view.mask if needed template like behaviour.

lottiViewTintView.backgroundColor = tintColor
lottieView = AnimationView(name: "animation_file")
lottieView.bounds = lottiViewTintView.bounds
lottiViewTintView.mask = lottieView
Kirow
  • 1,077
  • 12
  • 25
-3

You can customize lottie animation in lottie editor link. Lottie Animation Editor

Yadav-JI
  • 1
  • 2