Skip to content

Commit

Permalink
Add public animationLayer property to LottieAnimationLayer (#2103)
Browse files Browse the repository at this point in the history
  • Loading branch information
eromanc authored Jul 10, 2023
1 parent adef809 commit dd0854c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 33 deletions.
64 changes: 34 additions & 30 deletions Sources/Public/Animation/LottieAnimationLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,13 @@ public class LottieAnimationLayer: CALayer {
/// Will inform the receiver the type of rendering engine that is used for the layer.
public var animationLayerDidLoad:((_ animationLayer: LottieAnimationLayer, _ renderingEngine: RenderingEngineOption) -> Void)?

/// The underlying CALayer created to display the content.
/// Use this property to change CALayer props like the content's transform, anchor point, etc.
public var animationLayer: CALayer? { rootAnimationLayer }

public var screenScale: CGFloat {
didSet {
animationLayer?.renderScale = screenScale
rootAnimationLayer?.renderScale = screenScale
}
}

Expand Down Expand Up @@ -399,7 +403,7 @@ public class LottieAnimationLayer: CALayer {
/// Setting this will cause the animation to reload its image contents.
public var imageProvider: AnimationImageProvider {
didSet {
animationLayer?.imageProvider = imageProvider.cachedImageProvider
rootAnimationLayer?.imageProvider = imageProvider.cachedImageProvider
reloadImages()
}
}
Expand All @@ -408,15 +412,15 @@ public class LottieAnimationLayer: CALayer {
/// animation with values for text layers
public var textProvider: AnimationTextProvider {
didSet {
animationLayer?.textProvider = textProvider
rootAnimationLayer?.textProvider = textProvider
}
}

/// Sets the text provider for animation layer. A text provider provides the
/// animation with values for text layers
public var fontProvider: AnimationFontProvider {
didSet {
animationLayer?.fontProvider = fontProvider
rootAnimationLayer?.fontProvider = fontProvider
}
}

Expand All @@ -429,7 +433,7 @@ public class LottieAnimationLayer: CALayer {

/// Returns `true` if the animation is currently playing.
public var isAnimationPlaying: Bool {
guard let animationLayer = animationLayer else {
guard let animationLayer = rootAnimationLayer else {
return false
}

Expand Down Expand Up @@ -510,13 +514,13 @@ public class LottieAnimationLayer: CALayer {
updateAnimationFrame(newValue)
}
get {
animationLayer?.currentFrame ?? 0
rootAnimationLayer?.currentFrame ?? 0
}
}

/// Returns the current animation frame while an animation is playing.
public var realtimeAnimationFrame: AnimationFrameTime {
isAnimationPlaying ? animationLayer?.presentation()?.currentFrame ?? currentFrame : currentFrame
isAnimationPlaying ? rootAnimationLayer?.presentation()?.currentFrame ?? currentFrame : currentFrame
}

/// Returns the current animation frame while an animation is playing.
Expand All @@ -541,7 +545,7 @@ public class LottieAnimationLayer: CALayer {
/// Defaults to false
public var respectAnimationFrameRate = false {
didSet {
animationLayer?.respectAnimationFrameRate = respectAnimationFrameRate
rootAnimationLayer?.respectAnimationFrameRate = respectAnimationFrameRate
}
}

Expand All @@ -567,8 +571,8 @@ public class LottieAnimationLayer: CALayer {
}

public var animationView: LottieAnimationView? {
set { animationLayer?.animationView = newValue }
get { animationLayer?.animationView }
set { rootAnimationLayer?.animationView = newValue }
get { rootAnimationLayer?.animationView }
}

/// Sets the lottie file backing the animation layer. Setting this will clear the
Expand Down Expand Up @@ -607,12 +611,12 @@ public class LottieAnimationLayer: CALayer {

/// Reloads the images supplied to the animation from the `imageProvider`
public func reloadImages() {
animationLayer?.reloadImages()
rootAnimationLayer?.reloadImages()
}

/// Forces the LottieAnimationView to redraw its contents.
public func forceDisplayUpdate() {
animationLayer?.forceDisplayUpdate()
rootAnimationLayer?.forceDisplayUpdate()
}

/// Sets a ValueProvider for the specified keypath. The value provider will be set
Expand All @@ -638,7 +642,7 @@ public class LottieAnimationLayer: CALayer {
/// animationView.setValueProvider(redValueProvider, keypath: fillKeypath)
/// ```
public func setValueProvider(_ valueProvider: AnyValueProvider, keypath: AnimationKeypath) {
guard let animationLayer = animationLayer else { return }
guard let animationLayer = rootAnimationLayer else { return }

valueProviders[keypath] = valueProvider
animationLayer.setValueProvider(valueProvider, keypath: keypath)
Expand All @@ -650,7 +654,7 @@ public class LottieAnimationLayer: CALayer {
/// - Parameter for: The keypath used to search for the property.
/// - Parameter atFrame: The Frame Time of the value to query. If nil then the current frame is used.
public func getValue(for keypath: AnimationKeypath, atFrame: AnimationFrameTime?) -> Any? {
animationLayer?.getValue(for: keypath, atFrame: atFrame)
rootAnimationLayer?.getValue(for: keypath, atFrame: atFrame)
}

/// Reads the original value of a property specified by the Keypath.
Expand All @@ -660,18 +664,18 @@ public class LottieAnimationLayer: CALayer {
/// - Parameter for: The keypath used to search for the property.
/// - Parameter atFrame: The Frame Time of the value to query. If nil then the current frame is used.
public func getOriginalValue(for keypath: AnimationKeypath, atFrame: AnimationFrameTime?) -> Any? {
animationLayer?.getOriginalValue(for: keypath, atFrame: atFrame)
rootAnimationLayer?.getOriginalValue(for: keypath, atFrame: atFrame)
}

/// Logs all child keypaths.
public func logHierarchyKeypaths() {
animationLayer?.logHierarchyKeypaths()
rootAnimationLayer?.logHierarchyKeypaths()
}

/// Computes and returns a list of all child keypaths in the current animation.
/// The returned list is the same as the log output of `logHierarchyKeypaths()`
public func allHierarchyKeypaths() -> [String] {
animationLayer?.allHierarchyKeypaths() ?? []
rootAnimationLayer?.allHierarchyKeypaths() ?? []
}

/// Converts a CGRect from the LottieAnimationView's coordinate space into the
Expand All @@ -682,7 +686,7 @@ public class LottieAnimationLayer: CALayer {
/// - Parameter rect: The CGRect to convert.
/// - Parameter toLayerAt: The keypath used to find the layer.
public func convert(_ rect: CGRect, toLayerAt keypath: AnimationKeypath?) -> CGRect? {
guard let animationLayer = animationLayer else { return nil }
guard let animationLayer = rootAnimationLayer else { return nil }
guard let keypath = keypath else {
return convert(rect, to: animationLayer)
}
Expand All @@ -703,7 +707,7 @@ public class LottieAnimationLayer: CALayer {
/// - Parameter point: The CGPoint to convert.
/// - Parameter toLayerAt: The keypath used to find the layer.
public func convert(_ point: CGPoint, toLayerAt keypath: AnimationKeypath?) -> CGPoint? {
guard let animationLayer = animationLayer else { return nil }
guard let animationLayer = rootAnimationLayer else { return nil }
guard let keypath = keypath else {
return convert(point, to: animationLayer)
}
Expand All @@ -722,7 +726,7 @@ public class LottieAnimationLayer: CALayer {
/// - Parameter isEnabled: When true the animator nodes affect the rendering tree. When false the node is removed from the tree.
/// - Parameter keypath: The keypath used to find the node(s).
public func setNodeIsEnabled(isEnabled: Bool, keypath: AnimationKeypath) {
guard let animationLayer = animationLayer else { return }
guard let animationLayer = rootAnimationLayer else { return }
let nodes = animationLayer.animatorNodes(for: keypath)
if let nodes = nodes {
for node in nodes {
Expand Down Expand Up @@ -812,7 +816,7 @@ public class LottieAnimationLayer: CALayer {

// MARK: Internal

var animationLayer: RootAnimationLayer? = nil
var rootAnimationLayer: RootAnimationLayer? = nil

/// Context describing the animation that is currently playing in this `LottieAnimationView`
/// - When non-nil, an animation is currently playing in this layer. Otherwise,
Expand Down Expand Up @@ -848,17 +852,17 @@ public class LottieAnimationLayer: CALayer {
// with the ones managed by the performance test, and aren't actually
// necessary in a headless environment, so we disable them.
if TestHelpers.performanceTestsAreRunning {
animationLayer?.currentFrame = newFrame
animationLayer?.forceDisplayUpdate()
rootAnimationLayer?.currentFrame = newFrame
rootAnimationLayer?.forceDisplayUpdate()
return
}

CATransaction.begin()
CATransaction.setCompletionBlock {
self.animationLayer?.forceDisplayUpdate()
self.rootAnimationLayer?.forceDisplayUpdate()
}
CATransaction.setDisableActions(true)
animationLayer?.currentFrame = newFrame
rootAnimationLayer?.currentFrame = newFrame
CATransaction.commit()
}

Expand All @@ -883,8 +887,8 @@ public class LottieAnimationLayer: CALayer {

/// Remove current animation, and freeze the current frame.
let pauseFrame = realtimeAnimationFrame
animationLayer?.removeAnimation(forKey: activeAnimationName)
animationLayer?.currentFrame = pauseFrame
rootAnimationLayer?.removeAnimation(forKey: activeAnimationName)
rootAnimationLayer?.currentFrame = pauseFrame

addNewAnimationForContext(newContext)
}
Expand All @@ -895,7 +899,7 @@ public class LottieAnimationLayer: CALayer {
fileprivate var animationID = 0

fileprivate var activeAnimationName: String {
switch animationLayer?.primaryAnimationKey {
switch rootAnimationLayer?.primaryAnimationKey {
case .specific(let animationKey):
return animationKey
case .managed, nil:
Expand Down Expand Up @@ -942,7 +946,7 @@ public class LottieAnimationLayer: CALayer {
animationLayer.renderScale = screenScale

addSublayer(animationLayer)
self.animationLayer = animationLayer
self.rootAnimationLayer = animationLayer
reloadImages()
animationLayer.setNeedsDisplay()
setNeedsLayout()
Expand Down Expand Up @@ -1093,7 +1097,7 @@ public class LottieAnimationLayer: CALayer {

/// Adds animation to animation layer and sets the delegate. If animation layer or animation are nil, exits.
fileprivate func addNewAnimationForContext(_ animationContext: AnimationContext) {
guard let animationlayer = animationLayer, let animation = animation else {
guard let animationlayer = rootAnimationLayer, let animation = animation else {
return
}

Expand Down
5 changes: 2 additions & 3 deletions Sources/Public/Animation/LottieAnimationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ open class LottieAnimationView: LottieAnimationViewBase {
/// animationView.addSubview(subview, forLayerAt: layerKeypath)
/// ```
public func addSubview(_ subview: AnimationSubview, forLayerAt keypath: AnimationKeypath) {
guard let sublayer = lottieAnimationLayer.animationLayer?.layer(for: keypath) else {
guard let sublayer = lottieAnimationLayer.rootAnimationLayer?.layer(for: keypath) else {
return
}
setNeedsLayout()
Expand Down Expand Up @@ -723,7 +723,7 @@ open class LottieAnimationView: LottieAnimationViewBase {
// MARK: Internal

var animationLayer: RootAnimationLayer? {
lottieAnimationLayer.animationLayer
lottieAnimationLayer.rootAnimationLayer
}

/// Set animation name from Interface Builder
Expand Down Expand Up @@ -893,7 +893,6 @@ open class LottieAnimationView: LottieAnimationViewBase {
}

if shouldForceUpdates {
animationLayer.forceDisplayUpdate()
lottieAnimationLayer.forceDisplayUpdate()
}
}
Expand Down

0 comments on commit dd0854c

Please sign in to comment.