Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuring specific LottieView properties #2415

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Example/Example/LottieViewLayoutDemoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
33 changes: 33 additions & 0 deletions Sources/Public/Animation/LottieView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,42 @@ public struct LottieView<Placeholder: View>: 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.
freak4pc marked this conversation as resolved.
Show resolved Hide resolved
///
/// - 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>(
_ property: ReferenceWritableKeyPath<LottieAnimationView, Property>,
to value: Property)
-> Self
{
configure { $0[keyPath: property] = value }
freak4pc marked this conversation as resolved.
Show resolved Hide resolved
}

/// 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: Equatable>(
_ property: ReferenceWritableKeyPath<LottieAnimationView, Property>,
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
Expand Down
Loading