From 1be4f68269276468d0e166a1844da2b4aaf6f7a9 Mon Sep 17 00:00:00 2001 From: Ali <> Date: Mon, 9 Dec 2019 19:33:56 +0400 Subject: [PATCH] Support loading lottie animation from Data --- .../AnimationUI/Sources/AnimationNode.swift | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/submodules/AnimationUI/Sources/AnimationNode.swift b/submodules/AnimationUI/Sources/AnimationNode.swift index 026ad71701..9b896f983f 100644 --- a/submodules/AnimationUI/Sources/AnimationNode.swift +++ b/submodules/AnimationUI/Sources/AnimationNode.swift @@ -46,6 +46,35 @@ public final class AnimationNode : ASDisplayNode { }) } + public init(animationData: Data, colors: [String: UIColor]? = nil, scale: CGFloat = 1.0) { + self.scale = scale + + super.init() + + self.setViewBlock({ + if let json = try? JSONSerialization.jsonObject(with: animationData, options: []) as? [AnyHashable: Any] { + let composition = LOTComposition(json: json) + + let view = LOTAnimationView(model: composition, in: getAppBundle()) + view.animationSpeed = self.speed + view.backgroundColor = .clear + view.isOpaque = false + + if let colors = colors { + for (key, value) in colors { + let colorCallback = LOTColorValueCallback(color: value.cgColor) + self.colorCallbacks.append(colorCallback) + view.setValueDelegate(colorCallback, for: LOTKeypath(string: "\(key).Color")) + } + } + + return view + } else { + return LOTAnimationView() + } + }) + } + public func setAnimation(name: String) { if let url = getAppBundle().url(forResource: name, withExtension: "json"), let composition = LOTComposition(filePath: url.path) { self.animationView()?.sceneModel = composition