Skip to content

Commit

Permalink
Add option to remove animated bounds changes
Browse files Browse the repository at this point in the history
  • Loading branch information
thedrick committed Oct 18, 2023
1 parent 3421eeb commit 2cbf615
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions Sources/Public/Animation/LottieAnimationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ open class LottieAnimationView: LottieAnimationViewBase {
textProvider: AnimationKeypathTextProvider = DefaultTextProvider(),
fontProvider: AnimationFontProvider = DefaultFontProvider(),
configuration: LottieConfiguration = .shared,
logger: LottieLogger = .shared)
logger: LottieLogger = .shared,
animatesLayoutWithinCurrentContext: Bool = true)
{
lottieAnimationLayer = LottieAnimationLayer(
animation: animation,
Expand All @@ -113,6 +114,7 @@ open class LottieAnimationView: LottieAnimationViewBase {
configuration: configuration,
logger: logger)
self.logger = logger
self.animatesLayoutWithinCurrentContext = animatesLayoutWithinCurrentContext
super.init(frame: .zero)
commonInit()
if let animation = animation {
Expand All @@ -127,7 +129,8 @@ open class LottieAnimationView: LottieAnimationViewBase {
textProvider: AnimationKeypathTextProvider = DefaultTextProvider(),
fontProvider: AnimationFontProvider = DefaultFontProvider(),
configuration: LottieConfiguration = .shared,
logger: LottieLogger = .shared)
logger: LottieLogger = .shared,
animatesLayoutWithinCurrentContext: Bool = true)
{
lottieAnimationLayer = LottieAnimationLayer(
dotLottie: dotLottie,
Expand All @@ -137,6 +140,7 @@ open class LottieAnimationView: LottieAnimationViewBase {
configuration: configuration,
logger: logger)
self.logger = logger
self.animatesLayoutWithinCurrentContext = animatesLayoutWithinCurrentContext
super.init(frame: .zero)
commonInit()
if let animation = animation {
Expand All @@ -146,10 +150,12 @@ open class LottieAnimationView: LottieAnimationViewBase {

public init(
configuration: LottieConfiguration = .shared,
logger: LottieLogger = .shared)
logger: LottieLogger = .shared,
animatesLayoutWithinCurrentContext: Bool = true)
{
lottieAnimationLayer = LottieAnimationLayer(configuration: configuration, logger: logger)
self.logger = logger
self.animatesLayoutWithinCurrentContext = animatesLayoutWithinCurrentContext
super.init(frame: .zero)
commonInit()
}
Expand All @@ -163,6 +169,7 @@ open class LottieAnimationView: LottieAnimationViewBase {
configuration: .shared,
logger: .shared)
logger = .shared
animatesLayoutWithinCurrentContext = true
super.init(frame: frame)
commonInit()
}
Expand All @@ -176,6 +183,7 @@ open class LottieAnimationView: LottieAnimationViewBase {
configuration: .shared,
logger: .shared)
logger = .shared
animatesLayoutWithinCurrentContext = true
super.init(coder: aDecoder)
commonInit()
}
Expand All @@ -186,7 +194,8 @@ open class LottieAnimationView: LottieAnimationViewBase {
textProvider: AnimationKeypathTextProvider = DefaultTextProvider(),
fontProvider: AnimationFontProvider = DefaultFontProvider(),
configuration: LottieConfiguration = .shared,
logger: LottieLogger = .shared)
logger: LottieLogger = .shared,
animatesLayoutWithinCurrentContext: Bool = true)
{
switch animationSource {
case .lottieAnimation(let animation):
Expand All @@ -196,15 +205,17 @@ open class LottieAnimationView: LottieAnimationViewBase {
textProvider: textProvider,
fontProvider: fontProvider,
configuration: configuration,
logger: logger)
logger: logger,
animatesLayoutWithinCurrentContext: animatesLayoutWithinCurrentContext)

case .dotLottieFile(let dotLottieFile):
self.init(
dotLottie: dotLottieFile,
textProvider: textProvider,
fontProvider: fontProvider,
configuration: configuration,
logger: logger)
logger: logger,
animatesLayoutWithinCurrentContext: animatesLayoutWithinCurrentContext)

case nil:
self.init(
Expand All @@ -213,7 +224,8 @@ open class LottieAnimationView: LottieAnimationViewBase {
textProvider: textProvider,
fontProvider: fontProvider,
configuration: configuration,
logger: logger)
logger: logger,
animatesLayoutWithinCurrentContext: animatesLayoutWithinCurrentContext)
}
}

Expand Down Expand Up @@ -599,6 +611,13 @@ open class LottieAnimationView: LottieAnimationViewBase {
set { lottieAnimationLayer.mainThreadRenderingEngineShouldForceDisplayUpdateOnEachFrame = newValue }
}

/// Whether or not transform and position changes of the view should animate alongside
/// any existing animation context.
/// - Defaults to `true` which will grab the current animation context and animate position and
/// transform changes matching the current context's curve and duration.
/// `false` will cause transform and position changes to happen unanimated
public var animatesLayoutWithinCurrentContext: Bool

/// Sets the lottie file backing the animation view. Setting this will clear the
/// view's contents, completion blocks and current state. The new animation will
/// be loaded up and set to the beginning of its timeline.
Expand Down Expand Up @@ -933,7 +952,11 @@ open class LottieAnimationView: LottieAnimationViewBase {
// If layout is changed without animation, explicitly set animation duration to 0.0
// inside CATransaction to avoid unwanted artifacts.
/// Check if any animation exist on the view's layer, and match it.
if let key = lottieAnimationLayer.animationKeys()?.first, let animation = lottieAnimationLayer.animation(forKey: key) {
if
let key = lottieAnimationLayer.animationKeys()?.first,
let animation = lottieAnimationLayer.animation(forKey: key),
animatesLayoutWithinCurrentContext
{
// The layout is happening within an animation block. Grab the animation data.

let positionKey = "LayoutPositionAnimation"
Expand Down

0 comments on commit 2cbf615

Please sign in to comment.