1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-09-20 01:18:59 +03:00
Macaw/Source/animation/AnimationProducer.swift

127 lines
2.8 KiB
Swift
Raw Normal View History

import Swift_CAAnimation_Closure
2016-04-19 13:56:25 +03:00
2016-08-24 13:25:14 +03:00
let animationProducer = AnimationProducer()
class AnimationProducer {
2016-04-20 15:47:44 +03:00
2016-08-24 13:25:14 +03:00
public func addAnimation(animation: Animatable) {
2016-04-20 15:47:44 +03:00
2016-08-24 13:25:14 +03:00
guard let node = animation.node else {
return
}
2016-08-24 13:25:14 +03:00
guard let macawView = nodesMap.getView(node) else {
return
}
2016-04-20 15:47:44 +03:00
2016-08-24 13:25:14 +03:00
guard let cache = macawView.animationCache else {
return
}
2016-04-20 15:47:44 +03:00
2016-04-19 13:56:25 +03:00
switch animation.type {
case .Unknown:
return
case .AffineTransformation:
2016-08-24 13:25:14 +03:00
addTransformAnimation(animation, sceneLayer: macawView.layer, animationCache: cache, completion: {
if let next = animation.next {
self.addAnimation(next)
}
})
2016-08-02 14:35:52 +03:00
case .Opacity:
2016-08-24 13:25:14 +03:00
addOpacityAnimation(animation, sceneLayer: macawView.layer, animationCache: cache, completion: {
if let next = animation.next {
self.addAnimation(next)
}
})
2016-08-08 11:41:08 +03:00
case .Sequence:
addAnimationSequence(animation)
2016-08-08 13:23:15 +03:00
case .Combine:
addCombineAnimation(animation)
case .Empty:
executeCompletion(animation)
2016-04-19 13:56:25 +03:00
}
}
2016-08-08 11:41:08 +03:00
private func addAnimationSequence(animationSequnce: Animatable) {
guard let sequence = animationSequnce as? AnimationSequence else {
return
}
// Connecting animations
for i in 0..<(sequence.animations.count - 1) {
let animation = sequence.animations[i]
animation.next = sequence.animations[i + 1]
2016-08-08 11:41:08 +03:00
}
// Completion
if let completion = sequence.completion {
let completionAnimation = EmptyAnimation(completion: completion)
2016-08-23 10:52:40 +03:00
if let next = sequence.next {
completionAnimation.next = next
}
sequence.animations.last?.next = completionAnimation
2016-08-23 10:52:40 +03:00
} else {
if let next = sequence.next {
sequence.animations.last?.next = next
}
}
2016-08-08 11:41:08 +03:00
// Launching
if let firstAnimation = sequence.animations.first {
2016-08-08 11:41:08 +03:00
self.addAnimation(firstAnimation)
2016-08-08 11:41:08 +03:00
}
}
2016-08-08 13:23:15 +03:00
private func addCombineAnimation(combineAnimation: Animatable) {
guard let combine = combineAnimation as? CombineAnimation else {
return
}
2016-08-23 10:32:58 +03:00
// Looking for longest animation
var longestAnimation: Animatable?
combine.animations.forEach { animation in
guard let longest = longestAnimation else {
longestAnimation = animation
return
}
2016-08-23 10:32:58 +03:00
if longest.getDuration() < animation.getDuration() {
longestAnimation = animation
}
}
// Attaching completion empty animation and potential next animation
if let completion = combine.completion {
let completionAnimation = EmptyAnimation(completion: completion)
if let next = combine.next {
completionAnimation.next = next
}
longestAnimation?.next = completionAnimation
} else {
if let next = combine.next {
longestAnimation?.next = next
}
}
2016-08-09 13:18:11 +03:00
combine.removeFunc = {
combine.animations.forEach { animation in
animation.removeFunc?()
}
}
// Launching
2016-08-08 13:23:15 +03:00
combine.animations.forEach { animation in
self.addAnimation(animation)
}
}
2016-08-18 13:19:33 +03:00
private func executeCompletion(emptyAnimation: Animatable) {
emptyAnimation.completion?()
2016-08-08 13:23:15 +03:00
}
2016-05-17 16:27:31 +03:00
}