Skip to content

Value Providers Not Working Correctly with SwiftUI LottieView #2616

@mshll

Description

@mshll

Bug Description

When using value providers (specifically GradientValueProvider) with the native SwiftUI LottieView, gradients are not rendered correctly. The same code works perfectly fine with UIKit's LottieAnimationView.

Environment

  • Lottie Version: 4.5.2
  • iOS Version: 26.0 & 18.2
  • Xcode Version: 26.1.1

Expected Behavior

Gradient colors should be applied to the animation using GradientValueProvider, matching the behavior in UIKit.

Actual Behavior

  1. When gradient has fewer colors than the original animation (e.g., 2 colors when animation expects 3), the gradient renders as solid black
  2. When gradient color count matches the animation (e.g., 3 colors), only the first color renders as a solid color across the entire gradient
  3. Both issues only occur in SwiftUI, not in UIKit

Code to Reproduce

SwiftUI (NOT working):

import SwiftUI
import Lottie

struct SecondView: View {
    var body: some View {
        VStack(spacing: 20) {
            LottieView(animation: .named("19633-voice-video-switch"))
                .configure { view in
                    applyAnimationChanges(to: view)
                    view.logHierarchyKeypaths()
                }
                .playing(loopMode: .loop)
                .frame(maxWidth: .infinity, maxHeight: 400)
        }
    }
    
    private func applyAnimationChanges(to animationView: LottieAnimationView) {
        let color1 = LottieColor(r: 0/255, g: 80/255, b: 115/255, a: 1)
        let color2 = LottieColor(r: 113/255, g: 199/255, b: 236/255, a: 1)
        
        // This doesn't work in SwiftUI but works in UIKit
        let gradientValueProvider = GradientValueProvider([color1, color2])
        let gradientKeyPath = AnimationKeypath(keypath: "**.**.**.Colors")
        animationView.setValueProvider(gradientValueProvider, keypath: gradientKeyPath)
        
        let colorValueProvider = ColorValueProvider(color1)
        let footKeyPath = AnimationKeypath(keypath: "Foot.Rectangle 1.Stroke 1.Color")
        animationView.setValueProvider(colorValueProvider, keypath: footKeyPath)
    }
}

UIKit (Working correctly):

import UIKit
import Lottie

class SecondViewController: UIViewController {
    @IBOutlet weak var animationView: LottieAnimationView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let animation = LottieAnimation.named("19633-voice-video-switch")
        animationView.animation = animation
        animationView.contentMode = .scaleAspectFit
        animationView.loopMode = .loop
        
        applyAnimationChange()
        animationView.logHierarchyKeypaths()
        animationView.play()
    }
    
    private func applyAnimationChange() {
        let color1 = LottieColor(r: 0/255, g: 80/255, b: 115/255, a: 1)
        let color2 = LottieColor(r: 113/255, g: 199/255, b: 236/255, a: 1)
        
        // This works perfectly in UIKit
        let gradientValueProvider = GradientValueProvider([color1, color2])
        let gradientKeyPath = AnimationKeypath(keypath: "**.**.**.Colors")
        animationView.setValueProvider(gradientValueProvider, keypath: gradientKeyPath)
        
        let colorValueProvider = ColorValueProvider(color1)
        let footKeyPath = AnimationKeypath(keypath: "Foot.Rectangle 1.Stroke 1.Color")
        animationView.setValueProvider(colorValueProvider, keypath: footKeyPath)
    }
}

Additional Notes

The .configure modifier seems to execute at the right time (the closure is called), but value providers don't apply correctly. This appears to be a timing or initialization issue specific to the SwiftUI implementation.

Is there a recommended way to use value providers with the native SwiftUI LottieView?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions