diff --git a/Example/Example/LottieViewLayoutDemoView.swift b/Example/Example/LottieViewLayoutDemoView.swift index 8091ce4a1d..5b25856d06 100644 --- a/Example/Example/LottieViewLayoutDemoView.swift +++ b/Example/Example/LottieViewLayoutDemoView.swift @@ -9,10 +9,11 @@ struct LottieViewLayoutDemoView: View { HStack { VStack { LottieView(animation: .named("Samples/LottieLogo1")) + .configure(\.contentMode, to: .scaleAspectFit) .looping() .frame(maxWidth: 100) - Text("maxWidth: 100") + Text("maxWidth: 100, contentMode: .scaleAspectFit") } VStack { diff --git a/Sources/Public/Animation/LottieView.swift b/Sources/Public/Animation/LottieView.swift index f7a8ef1e53..aae74dfd46 100644 --- a/Sources/Public/Animation/LottieView.swift +++ b/Sources/Public/Animation/LottieView.swift @@ -141,9 +141,42 @@ public struct LottieView: UIViewConfiguringSwiftUIView { } } + /// Returns a copy of this `LottieView` updated to have the specific configuration property + /// applied to its represented `LottieAnimationView` whenever it is updated via the `updateUIView(...)` + /// or `updateNSView(...)` methods. + /// + /// - note: This configuration will be applied on every SwiftUI render pass. + /// Be wary of applying heavy side-effects as configuration values. + public func configure( + _ property: ReferenceWritableKeyPath, + to value: Property) + -> Self + { + configure { $0[keyPath: property] = value } + } + + /// Returns a copy of this `LottieView` updated to have the specific configuration property + /// applied to its represented `LottieAnimationView` whenever it is updated via the `updateUIView(...)` + /// or `updateNSView(...)` methods. + /// + /// - note: If the `value` is already the currently-applied configuration value, it won't be applied + public func configure( + _ property: ReferenceWritableKeyPath, + to value: Property) + -> Self + { + configure { + guard $0[keyPath: property] != value else { return } + $0[keyPath: property] = value + } + } + /// Returns a copy of this `LottieView` updated to have the given closure applied to its /// represented `LottieAnimationView` whenever it is updated via the `updateUIView(…)` /// or `updateNSView(…)` method. + /// + /// - note: This configuration closure will be executed on every SwiftUI render pass. + /// Be wary of applying heavy side-effects inside it. public func configure(_ configure: @escaping (LottieAnimationView) -> Void) -> Self { var copy = self copy.configurations.append { context in