diff --git a/Source/animation/AnimationImpl.swift b/Source/animation/AnimationImpl.swift index fa5cd5b3..0b79032e 100644 --- a/Source/animation/AnimationImpl.swift +++ b/Source/animation/AnimationImpl.swift @@ -22,7 +22,16 @@ enum AnimationType { class BasicAnimation: Animation { - weak var node: Node? + weak var node: Node? { + didSet { + node?.animations.append(self) + if let group = node as? Group { + for node in group.contents { + node.animations.append(self) + } + } + } + } weak var nodeRenderer: NodeRenderer? var type = AnimationType.unknown let ID: String diff --git a/Source/animation/AnimationProducer.swift b/Source/animation/AnimationProducer.swift index af6e9ebd..78dac66e 100644 --- a/Source/animation/AnimationProducer.swift +++ b/Source/animation/AnimationProducer.swift @@ -284,6 +284,10 @@ class AnimationProducer { continue } + defer { + renderer.sceneLayer?.setNeedsDisplay() + } + let progress = currentDate.timeIntervalSince(animationDesc.startDate) / animation.duration + animation.pausedProgress // Completion diff --git a/Source/animation/types/animation_generators/Cache/AnimationCache.swift b/Source/animation/types/animation_generators/Cache/AnimationCache.swift index f2fc9527..f09a9317 100644 --- a/Source/animation/types/animation_generators/Cache/AnimationCache.swift +++ b/Source/animation/types/animation_generators/Cache/AnimationCache.swift @@ -157,6 +157,17 @@ class AnimationUtils { } } +extension Node { + + func isAnimating() -> Bool { + return !animations.filter { $0.state() == AnimationState.running }.isEmpty + } + + func needsLayer() -> Bool { + return !animations.filter { $0.state() == AnimationState.running || $0.state() == AnimationState.initial }.isEmpty + } +} + extension NodeRenderer { func isAnimating() -> Bool { @@ -166,7 +177,7 @@ extension NodeRenderer { func freeLayer() { let nodeRenderer = self - guard let layer = nodeRenderer.layer else { + guard let layer = nodeRenderer.layer, !node.needsLayer() else { return } nodeRenderer.layer = nil diff --git a/Source/animation/types/animation_generators/OpacityGenerator.swift b/Source/animation/types/animation_generators/OpacityGenerator.swift index c91c1942..71e9441c 100644 --- a/Source/animation/types/animation_generators/OpacityGenerator.swift +++ b/Source/animation/types/animation_generators/OpacityGenerator.swift @@ -34,8 +34,6 @@ func addOpacityAnimation(_ animation: BasicAnimation, _ context: AnimationContex generatedAnimation.completion = { finished in - renderer.freeLayer() - if animation.paused { animation.pausedProgress += animation.progress node.opacityVar.value = opacityAnimation.getVFunc()(animation.pausedProgress) @@ -49,6 +47,8 @@ func addOpacityAnimation(_ animation: BasicAnimation, _ context: AnimationContex node.opacityVar.value = opacityAnimation.getVFunc()(1.0) } + renderer.freeLayer() + if !animation.cycled && !animation.manualStop && !animation.paused { diff --git a/Source/model/scene/Node.swift b/Source/model/scene/Node.swift index 2c5282bf..0f321552 100644 --- a/Source/model/scene/Node.swift +++ b/Source/model/scene/Node.swift @@ -38,6 +38,8 @@ open class Node: Drawable { set(val) { effectVar.value = val } } + var animations: [BasicAnimation] = [] + // MARK: - Searching public func nodeBy(tag: String) -> Node? { if self.tag.contains(tag) { diff --git a/Source/render/NodeRenderer.swift b/Source/render/NodeRenderer.swift index 87240b55..20f9ae0b 100644 --- a/Source/render/NodeRenderer.swift +++ b/Source/render/NodeRenderer.swift @@ -76,9 +76,9 @@ class NodeRenderer { self.parentRenderer = parentRenderer onNodeChange = { [weak view] in - // if self.isAnimating() { - // return - // } + if node.isAnimating() { + return + } view?.setNeedsDisplay() }