2016-05-16 14:17:55 +03:00
|
|
|
import Swift_CAAnimation_Closure
|
2016-04-19 13:56:25 +03:00
|
|
|
|
2016-08-24 13:25:14 +03:00
|
|
|
let animationProducer = AnimationProducer()
|
2016-08-18 12:53:33 +03:00
|
|
|
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-23 09:47:52 +03:00
|
|
|
|
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: {
|
2016-08-23 09:47:52 +03:00
|
|
|
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: {
|
2016-08-23 09:47:52 +03:00
|
|
|
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)
|
2016-08-23 09:47:52 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-08-23 09:47:52 +03:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2016-08-23 09:47:52 +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
|
|
|
|
}
|
|
|
|
|
2016-08-23 09:47:52 +03:00
|
|
|
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-23 09:47:52 +03:00
|
|
|
}
|
2016-08-08 11:41:08 +03:00
|
|
|
|
2016-08-23 09:47:52 +03:00
|
|
|
// Launching
|
|
|
|
if let firstAnimation = sequence.animations.first {
|
2016-08-08 11:41:08 +03:00
|
|
|
|
2016-08-23 09:47:52 +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 09:47:52 +03:00
|
|
|
|
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-23 09:47:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-09 13:18:11 +03:00
|
|
|
combine.removeFunc = {
|
|
|
|
combine.animations.forEach { animation in
|
|
|
|
animation.removeFunc?()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 09:47:52 +03:00
|
|
|
// Launching
|
2016-08-08 13:23:15 +03:00
|
|
|
combine.animations.forEach { animation in
|
|
|
|
self.addAnimation(animation)
|
|
|
|
}
|
2016-08-23 09:47:52 +03:00
|
|
|
}
|
2016-08-18 13:19:33 +03:00
|
|
|
|
2016-08-23 09:47:52 +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
|
|
|
}
|