Commit f4a5cc8c by Daniel Dahan

development: simplified MotionAnimation API

parent a9620296
...@@ -282,7 +282,7 @@ extension CALayer { ...@@ -282,7 +282,7 @@ extension CALayer {
} else { } else {
let a = Motion.shadow(path: UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath) let a = Motion.shadow(path: UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath)
a.fromValue = shadowPath a.fromValue = shadowPath
animate(animation: a) animate(a)
} }
} }
} }
...@@ -53,9 +53,9 @@ public enum MotionAnimationKeyPath: String { ...@@ -53,9 +53,9 @@ public enum MotionAnimationKeyPath: String {
} }
public enum MotionAnimation { public enum MotionAnimation {
case delay(CGFloat) case delay(TimeInterval)
case timingFunction(MotionAnimationTimingFunction) case timingFunction(MotionAnimationTimingFunction)
case duration(CGFloat) case duration(TimeInterval)
case custom(CABasicAnimation) case custom(CABasicAnimation)
case backgroundColor(UIColor) case backgroundColor(UIColor)
case corners(CGFloat) case corners(CGFloat)
...@@ -89,9 +89,18 @@ extension CALayer { ...@@ -89,9 +89,18 @@ extension CALayer {
view's backing layer. view's backing layer.
- Parameter animation: A CAAnimation instance. - Parameter animation: A CAAnimation instance.
*/ */
open func animate(animation: CAAnimation) { open func animate(_ animations: CAAnimation...) {
animation.delegate = self animate(animations)
}
/**
A method that accepts CAAnimation objects and executes them on the
view's backing layer.
- Parameter animation: A CAAnimation instance.
*/
open func animate(_ animations: [CAAnimation]) {
for animation in animations {
animation.delegate = self
if let a = animation as? CABasicAnimation { if let a = animation as? CABasicAnimation {
a.fromValue = (presentation() ?? self).value(forKeyPath: a.keyPath!) a.fromValue = (presentation() ?? self).value(forKeyPath: a.keyPath!)
} }
...@@ -104,6 +113,7 @@ extension CALayer { ...@@ -104,6 +113,7 @@ extension CALayer {
add(a, forKey: kCATransition) add(a, forKey: kCATransition)
} }
} }
}
/** /**
A delegation method that is executed when the backing layer stops A delegation method that is executed when the backing layer stops
...@@ -139,80 +149,40 @@ extension CALayer { ...@@ -139,80 +149,40 @@ extension CALayer {
removeAnimation(forKey: k) removeAnimation(forKey: k)
} }
open func motion(animations: MotionAnimation...) { open func motion(_ animations: MotionAnimation...) {
motion(animations: animations) motion(animations)
} }
open func motion(animations: [MotionAnimation]) { open func motion(_ animations: [MotionAnimation]) {
motion(duration: 0.25, animations: animations) motion(delay: 0, duration: 0.25, timingFunction: .easeInEaseOut, animations: animations)
} }
open func motion(duration: TimeInterval, animations: MotionAnimation...) { fileprivate func motion(delay: TimeInterval, duration: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: [MotionAnimation]) {
motion(duration: duration, animations: animations) var t = delay
}
open func motion(duration: TimeInterval, animations: [MotionAnimation]) {
motion(duration: duration, timingFunction: .easeInEaseOut, animations: animations)
}
open func motion(timingFunction: MotionAnimationTimingFunction, animations: MotionAnimation...) {
motion(timingFunction: timingFunction, animations: animations)
}
open func motion(timingFunction: MotionAnimationTimingFunction, animations: [MotionAnimation]) {
motion(duration: 0.25, animations: animations)
}
open func motion(duration: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: MotionAnimation...) {
motion(duration: duration, timingFunction: timingFunction, animations: animations)
}
open func motion(duration: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: [MotionAnimation]) {
motion(delay: 0, duration: duration, timingFunction: timingFunction, animations: animations)
}
open func motion(delay: TimeInterval, animations: MotionAnimation...) {
motion(delay: delay, animations: animations)
}
open func motion(delay: TimeInterval, animations: [MotionAnimation]) {
motion(delay: delay, duration: 0.25, timingFunction: .easeInEaseOut, animations: animations)
}
open func motion(delay: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: MotionAnimation...) {
motion(delay: delay, timingFunction: timingFunction, animations: animations)
}
open func motion(delay: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: [MotionAnimation]) {
motion(delay: delay, timingFunction: timingFunction, animations: animations)
}
open func motion(delay: TimeInterval, duration: TimeInterval, animations: MotionAnimation...) { for v in animations {
motion(delay: delay, duration: duration, timingFunction: .easeInEaseOut, animations: animations) switch v {
case let .delay(time):
t = time
default:break
} }
open func motion(delay: TimeInterval, duration: TimeInterval, animations: [MotionAnimation]) {
motion(delay: delay, duration: duration, timingFunction: .easeInEaseOut, animations: animations)
} }
open func motion(delay: TimeInterval, duration: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: MotionAnimation...) { Motion.delay(time: t) { [weak self] in
motion(delay: delay, duration: duration, timingFunction: timingFunction, animations: animations) guard let s = self else {
return
} }
open func motion(delay: TimeInterval, duration: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: [MotionAnimation]) {
var a = [CABasicAnimation]() var a = [CABasicAnimation]()
var tf = timingFunction var tf = timingFunction
var d = duration var d = duration
var t = delay
for v in animations { for v in animations {
switch v { switch v {
case let .delay(time):
t = TimeInterval(time)
case let .timingFunction(tFunction): case let .timingFunction(tFunction):
tf = tFunction tf = tFunction
case let .duration(dur): case let .duration(dur):
d = TimeInterval(dur) d = dur
case let .custom(animation): case let .custom(animation):
a.append(animation) a.append(animation)
case let .backgroundColor(color): case let .backgroundColor(color):
...@@ -259,26 +229,22 @@ extension CALayer { ...@@ -259,26 +229,22 @@ extension CALayer {
a.append(Motion.shadow(path: path)) a.append(Motion.shadow(path: path))
case let .fade(opacity): case let .fade(opacity):
let fade = Motion.fade(opacity: opacity) let fade = Motion.fade(opacity: opacity)
fade.fromValue = value(forKey: MotionAnimationKeyPath.opacity.rawValue) ?? NSNumber(floatLiteral: 1) fade.fromValue = s.value(forKey: MotionAnimationKeyPath.opacity.rawValue) ?? NSNumber(floatLiteral: 1)
a.append(fade) a.append(fade)
case let .zPosition(index): case let .zPosition(index):
let zPosition = Motion.zPosition(index: index) let zPosition = Motion.zPosition(index: index)
zPosition.fromValue = value(forKey: MotionAnimationKeyPath.zPosition.rawValue) ?? NSNumber(integerLiteral: 0) zPosition.fromValue = s.value(forKey: MotionAnimationKeyPath.zPosition.rawValue) ?? NSNumber(integerLiteral: 0)
a.append(zPosition) a.append(zPosition)
default:break
} }
} }
Motion.delay(time: t) { [weak self] in
guard let s = self else {
return
}
let g = Motion.animate(group: a, duration: d) let g = Motion.animate(group: a, duration: d)
g.fillMode = MotionAnimationFillModeToValue(mode: .forwards) g.fillMode = MotionAnimationFillModeToValue(mode: .forwards)
g.isRemovedOnCompletion = false g.isRemovedOnCompletion = false
g.timingFunction = MotionAnimationTimingFunctionToValue(timingFunction: tf) g.timingFunction = MotionAnimationTimingFunctionToValue(timingFunction: tf)
s.animate(animation: g) s.animate(g)
} }
} }
} }
...@@ -292,72 +258,20 @@ extension UIView { ...@@ -292,72 +258,20 @@ extension UIView {
view's backing layer. view's backing layer.
- Parameter animation: A CAAnimation instance. - Parameter animation: A CAAnimation instance.
*/ */
open func animate(animation: CAAnimation) { open func animate(_ animations: CAAnimation...) {
layer.animate(animation: animation) layer.animate(animations)
}
open func motion(animations: MotionAnimation...) {
layer.motion(animations: animations)
}
open func motion(animations: [MotionAnimation]) {
layer.motion(animations: animations)
}
open func motion(duration: TimeInterval, animations: MotionAnimation...) {
layer.motion(duration: duration, animations: animations)
}
open func motion(duration: TimeInterval, animations: [MotionAnimation]) {
layer.motion(duration: duration, animations: animations)
}
open func motion(timingFunction: MotionAnimationTimingFunction, animations: MotionAnimation...) {
layer.motion(timingFunction: timingFunction, animations: animations)
}
open func motion(timingFunction: MotionAnimationTimingFunction, animations: [MotionAnimation]) {
layer.motion(timingFunction: timingFunction, animations: animations)
}
open func motion(duration: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: MotionAnimation...) {
layer.motion(duration: duration, timingFunction: timingFunction, animations: animations)
}
open func motion(duration: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: [MotionAnimation]) {
layer.motion(duration: duration, timingFunction: timingFunction, animations: animations)
}
open func motion(delay: TimeInterval, animations: MotionAnimation...) {
layer.motion(delay: delay, animations: animations)
}
open func motion(delay: TimeInterval, animations: [MotionAnimation]) {
layer.motion(delay: delay, animations: animations)
}
open func motion(delay: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: MotionAnimation...) {
layer.motion(delay: delay, timingFunction: timingFunction, animations: animations)
}
open func motion(delay: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: [MotionAnimation]) {
layer.motion(delay: delay, timingFunction: timingFunction, animations: animations)
}
open func motion(delay: TimeInterval, duration: TimeInterval, animations: MotionAnimation...) {
layer.motion(delay: delay, duration: duration, animations: animations)
} }
open func motion(delay: TimeInterval, duration: TimeInterval, animations: [MotionAnimation]) { open func animate(_ animations: [CAAnimation]) {
layer.motion(delay: delay, duration: duration, animations: animations) layer.animate(animations)
} }
open func motion(delay: TimeInterval, duration: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: MotionAnimation...) { open func motion(_ animations: MotionAnimation...) {
layer.motion(delay: delay, duration: duration, timingFunction: timingFunction, animations: animations) layer.motion(animations)
} }
open func motion(delay: TimeInterval, duration: TimeInterval, timingFunction: MotionAnimationTimingFunction, animations: [MotionAnimation]) { open func motion(_ animations: [MotionAnimation]) {
layer.motion(delay: delay, duration: duration, timingFunction: timingFunction, animations: animations) layer.motion(animations)
} }
} }
......
...@@ -152,7 +152,7 @@ open class SlideMotionTransition: NSObject, UIViewControllerAnimatedTransitionin ...@@ -152,7 +152,7 @@ open class SlideMotionTransition: NSObject, UIViewControllerAnimatedTransitionin
if 0 < n.motionIdentifier.utf16.count { if 0 < n.motionIdentifier.utf16.count {
for m in toView.subviews { for m in toView.subviews {
if n.motionIdentifier == m.motionIdentifier { if n.motionIdentifier == m.motionIdentifier {
m.motion(duration: duration, animations: m.motionAnimations) m.motion(m.motionAnimations)
} }
} }
} }
...@@ -168,7 +168,7 @@ open class SlideMotionTransition: NSObject, UIViewControllerAnimatedTransitionin ...@@ -168,7 +168,7 @@ open class SlideMotionTransition: NSObject, UIViewControllerAnimatedTransitionin
if 0 < n.motionIdentifier.utf16.count { if 0 < n.motionIdentifier.utf16.count {
for m in toView.subviews { for m in toView.subviews {
if n.motionIdentifier == m.motionIdentifier { if n.motionIdentifier == m.motionIdentifier {
m.motion(duration: duration, animations: m.motionAnimations) m.motion(m.motionAnimations)
} }
} }
} }
......
...@@ -146,13 +146,13 @@ extension PulseMotion { ...@@ -146,13 +146,13 @@ extension PulseMotion {
switch animation { switch animation {
case .centerWithBacking, .backing, .pointWithBacking, .tap: case .centerWithBacking, .backing, .pointWithBacking, .tap:
bLayer.motion(duration: duration, animations: .backgroundColor(color.withAlphaComponent(opacity / 2))) bLayer.motion(.duration(duration), .backgroundColor(color.withAlphaComponent(opacity / 2)))
default:break default:break
} }
switch animation { switch animation {
case .center, .centerWithBacking, .centerRadialBeyondBounds, .radialBeyondBounds, .point, .pointWithBacking: case .center, .centerWithBacking, .centerRadialBeyondBounds, .radialBeyondBounds, .point, .pointWithBacking:
pLayer.motion(duration: duration, animations: .scale(1)) pLayer.motion(.duration(duration), .scale(1))
default:break default:break
} }
...@@ -182,13 +182,13 @@ extension PulseMotion { ...@@ -182,13 +182,13 @@ extension PulseMotion {
switch animation { switch animation {
case .centerWithBacking, .backing, .pointWithBacking, .tap: case .centerWithBacking, .backing, .pointWithBacking, .tap:
bLayer.motion(duration: duration, animations: .backgroundColor(color.withAlphaComponent(0))) bLayer.motion(.duration(duration), .backgroundColor(color.withAlphaComponent(0)))
default:break default:break
} }
switch animation { switch animation {
case .center, .centerWithBacking, .centerRadialBeyondBounds, .radialBeyondBounds, .point, .pointWithBacking: case .center, .centerWithBacking, .centerRadialBeyondBounds, .radialBeyondBounds, .point, .pointWithBacking:
pLayer.motion(duration: duration, animations: .scale(.center == animation ? 1 : 1.325), .backgroundColor(color.withAlphaComponent(0))) pLayer.motion(.duration(duration), .scale(.center == animation ? 1 : 1.325), .backgroundColor(color.withAlphaComponent(0)))
default:break default:break
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment