From 4a66046ef25296b962fd454517bb7672d06a1b6e Mon Sep 17 00:00:00 2001 From: Giulia Ariu <> Date: Wed, 14 Sep 2022 17:34:33 +0200 Subject: [PATCH] Added new environment key to be able to change the animation progress --- .../LottieAnimationProgressEnvironmentKey.swift | 14 ++++++++++++++ Sources/LottieExtensions/LottieView.swift | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 Sources/LottieExtensions/LottieAnimationProgressEnvironmentKey.swift diff --git a/Sources/LottieExtensions/LottieAnimationProgressEnvironmentKey.swift b/Sources/LottieExtensions/LottieAnimationProgressEnvironmentKey.swift new file mode 100644 index 0000000..8b1a8c8 --- /dev/null +++ b/Sources/LottieExtensions/LottieAnimationProgressEnvironmentKey.swift @@ -0,0 +1,14 @@ +// Copyright © 2022 Lautsprecher Teufel GmbH. All rights reserved. + +import SwiftUI + +extension EnvironmentValues { + var lottieAnimationProgress: CGFloat { + get { return self[LottieAnimationProgressEnvironmentKey.self] } + set { self[LottieAnimationProgressEnvironmentKey.self] = newValue } + } +} + +public struct LottieAnimationProgressEnvironmentKey: EnvironmentKey { + public static let defaultValue: CGFloat = 0.5 +} diff --git a/Sources/LottieExtensions/LottieView.swift b/Sources/LottieExtensions/LottieView.swift index 23c2d25..cee3df9 100644 --- a/Sources/LottieExtensions/LottieView.swift +++ b/Sources/LottieExtensions/LottieView.swift @@ -17,6 +17,7 @@ public struct LottieView: UIViewRepresentable { let bundle: Bundle let loopMode: LottieLoopMode let completion: LottieCompletionBlock? + @Environment(\.lottieAnimationProgress) var lottieAnimationProgress public init(filename: String, bundle: Bundle, loopMode: LottieLoopMode = .playOnce, completion: LottieCompletionBlock? = nil) { self.filename = filename @@ -33,7 +34,7 @@ public struct LottieView: UIViewRepresentable { animationView.contentMode = .scaleAspectFit animationView.backgroundBehavior = .pauseAndRestore if context.environment.disableAnimations { - animationView.currentProgress = 0.5 + animationView.currentProgress = lottieAnimationProgress } else { animationView.play(completion: completion) }