1
1
mirror of https://github.com/exyte/Macaw.git synced 2024-11-13 13:14:20 +03:00

Simple morphing animation generator

This commit is contained in:
Viktor Sukochev 2017-01-25 17:31:41 +07:00
parent b99f830f50
commit 4ac6f1e2c4
3 changed files with 66 additions and 2 deletions

View File

@ -5,6 +5,7 @@ public protocol LocusInterpolation: Interpolable {
extension Locus: LocusInterpolation {
public func interpolate(_ endValue: Locus, progress: Double) -> Self {
return self
}
}

View File

@ -4,7 +4,7 @@ class MorphingAnimation: AnimationImpl<Locus> {
convenience init(animatedNode: Shape, startValue: Locus, finalValue: Locus, animationDuration: Double, delay: Double = 0.0, autostart: Bool = false, fps: UInt = 30) {
let interpolationFunc = { (t: Double) -> Locus in
return startValue.interpolate(finalValue, progress: t)
return finalValue//startValue.interpolate(finalValue, progress: t)
}
self.init(animatedNode: animatedNode, valueFunc: interpolationFunc, animationDuration: animationDuration, delay: delay, autostart: autostart, fps: fps)
@ -54,7 +54,7 @@ public extension AnimatableVariable where T: MorphingAnimationDescription {
let origin = (node as! Shape).form
let factory = { () -> (Double) -> Locus in
return { (t: Double) in return origin.interpolate(to, progress: t) }
return { (t: Double) in return to }//origin.interpolate(to, progress: t) }
}
return MorphingAnimation(animatedNode: self.node as! Shape, factory: factory, animationDuration: during, delay: delay)

View File

@ -7,4 +7,67 @@
//
func addMorphingAnimation(_ animation: BasicAnimation, sceneLayer: CALayer, animationCache: AnimationCache, completion: @escaping (() -> ())) {
guard let morphingAnimation = animation as? MorphingAnimation else {
return
}
guard let shape = animation.node as? Shape else {
return
}
let toLocus = morphingAnimation.getVFunc()(1.0)
// Creating proper animation
let generatedAnim = pathAnimation(toLocus:toLocus, duration: animation.getDuration())
generatedAnim.repeatCount = Float(animation.repeatCount)
generatedAnim.timingFunction = caTimingFunction(animation.easing)
generatedAnim.completion = { finished in
if !animation.manualStop {
animation.progress = 1.0
shape.form = morphingAnimation.getVFunc()(1.0)
} else {
shape.form = morphingAnimation.getVFunc()(animation.progress)
}
animationCache.freeLayer(shape)
animation.completion?()
if !finished {
animationRestorer.addRestoreClosure(completion)
return
}
completion()
}
generatedAnim.progress = { progress in
let t = Double(progress)
shape.form = morphingAnimation.getVFunc()(t)
animation.progress = t
animation.onProgressUpdate?(t)
}
let layer = animationCache.layerForNode(shape, animation: animation)
layer.add(generatedAnim, forKey: animation.ID)
animation.removeFunc = {
layer.removeAnimation(forKey: animation.ID)
}
}
fileprivate func pathAnimation(toLocus: Locus, duration: Double) -> CAAnimation {
let toPath = RenderUtils.toCGPath(toLocus)
let animation = CABasicAnimation(keyPath: "path")
animation.toValue = toPath
animation.duration = duration
return animation
}