Commit aad47c6d by Daniel Dahan

added first pass at Spring animmations

parent a8b1aa0e
......@@ -42,6 +42,7 @@
96AEB6AF1EE4610F009A3BE0 /* MatchPreprocessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96AEB68B1EE4610F009A3BE0 /* MatchPreprocessor.swift */; };
96AEB6B01EE4610F009A3BE0 /* SourcePreprocessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96AEB68C1EE4610F009A3BE0 /* SourcePreprocessor.swift */; };
96E49A401EEA08F8006D5A93 /* MotionTransitionObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96E49A3F1EEA08F8006D5A93 /* MotionTransitionObserver.swift */; };
96F8905B1F045099004BEB1A /* MotionCAAnimation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 96F8905A1F045099004BEB1A /* MotionCAAnimation.swift */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
......@@ -83,6 +84,7 @@
96C98DE21E43809D00B22906 /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
96C98DED1E438A5700B22906 /* Motion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Motion.h; sourceTree = "<group>"; };
96E49A3F1EEA08F8006D5A93 /* MotionTransitionObserver.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MotionTransitionObserver.swift; sourceTree = "<group>"; };
96F8905A1F045099004BEB1A /* MotionCAAnimation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MotionCAAnimation.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
......@@ -173,6 +175,7 @@
96AEB67B1EE4610F009A3BE0 /* MotionTransition.swift */,
96E49A3F1EEA08F8006D5A93 /* MotionTransitionObserver.swift */,
96AEB67D1EE4610F009A3BE0 /* MotionPlugin.swift */,
96F8905A1F045099004BEB1A /* MotionCAAnimation.swift */,
96AEB6641EE4610F009A3BE0 /* Animator */,
96AEB66D1EE4610F009A3BE0 /* Extensions */,
96AEB6861EE4610F009A3BE0 /* Preprocessors */,
......@@ -267,6 +270,7 @@
96AEB6931EE4610F009A3BE0 /* TransitionPreprocessor.swift in Sources */,
96AEB68F1EE4610F009A3BE0 /* MotionDefaultAnimator.swift in Sources */,
96AEB69D1EE4610F009A3BE0 /* Motion.swift in Sources */,
96F8905B1F045099004BEB1A /* MotionCAAnimation.swift in Sources */,
963150D61EE51C7A002B0D42 /* MotionAnimation.swift in Sources */,
968989DE1EE6633E003B8F3D /* MotionAnimator.swift in Sources */,
968989B91EE5B34B003B8F3D /* MotionHasInsertOrder.swift in Sources */,
......
......@@ -160,102 +160,108 @@ fileprivate extension CALayer {
let d: TimeInterval = targetState.duration ?? duration
if let v = targetState.backgroundColor {
let a = MotionBasicAnimation.background(color: UIColor(cgColor: v))
let a = MotionCAAnimation.background(color: UIColor(cgColor: v))
a.fromValue = s.backgroundColor
anims.append(a)
}
if let v = targetState.borderColor {
let a = MotionBasicAnimation.border(color: UIColor(cgColor: v))
let a = MotionCAAnimation.border(color: UIColor(cgColor: v))
a.fromValue = s.borderColor
anims.append(a)
}
if let v = targetState.borderWidth {
let a = MotionBasicAnimation.border(width: v)
let a = MotionCAAnimation.border(width: v)
a.fromValue = NSNumber(floatLiteral: Double(s.borderWidth))
anims.append(a)
}
if let v = targetState.cornerRadius {
let a = MotionBasicAnimation.corner(radius: v)
let a = MotionCAAnimation.corner(radius: v)
a.fromValue = NSNumber(floatLiteral: Double(s.cornerRadius))
anims.append(a)
}
if let v = targetState.transform {
let a = MotionBasicAnimation.transform(v)
let a = MotionCAAnimation.transform(v)
a.fromValue = NSValue(caTransform3D: s.transform)
anims.append(a)
}
if let v = targetState.spin {
var a = MotionBasicAnimation.spinX(v.0)
var a = MotionCAAnimation.spinX(v.0)
a.fromValue = NSNumber(floatLiteral: 0)
anims.append(a)
a = MotionBasicAnimation.spinY(v.1)
a = MotionCAAnimation.spinY(v.1)
a.fromValue = NSNumber(floatLiteral: 0)
anims.append(a)
a = MotionBasicAnimation.spinZ(v.2)
a = MotionCAAnimation.spinZ(v.2)
a.fromValue = NSNumber(floatLiteral: 0)
anims.append(a)
}
if let v = targetState.position {
let a = MotionBasicAnimation.position(v)
let a = MotionCAAnimation.position(v)
a.fromValue = NSValue(cgPoint: s.position)
anims.append(a)
}
if let v = targetState.opacity {
let a = MotionBasicAnimation.fade(v)
let a = MotionCAAnimation.fade(v)
a.fromValue = s.value(forKeyPath: MotionAnimationKeyPath.opacity.rawValue) ?? NSNumber(floatLiteral: 1)
anims.append(a)
}
if let v = targetState.zPosition {
let a = MotionBasicAnimation.zPosition(v)
let a = MotionCAAnimation.zPosition(v)
a.fromValue = s.value(forKeyPath: MotionAnimationKeyPath.zPosition.rawValue) ?? NSNumber(floatLiteral: 0)
anims.append(a)
}
if let v = targetState.size {
let a = MotionBasicAnimation.size(v)
let a = MotionCAAnimation.size(v)
a.fromValue = NSValue(cgSize: s.bounds.size)
anims.append(a)
}
if let v = targetState.shadowPath {
let a = MotionBasicAnimation.shadow(path: v)
let a = MotionCAAnimation.shadow(path: v)
a.fromValue = s.shadowPath
anims.append(a)
}
if let v = targetState.shadowColor {
let a = MotionBasicAnimation.shadow(color: UIColor(cgColor: v))
let a = MotionCAAnimation.shadow(color: UIColor(cgColor: v))
a.fromValue = s.shadowColor
anims.append(a)
}
if let v = targetState.shadowOffset {
let a = MotionBasicAnimation.shadow(offset: v)
let a = MotionCAAnimation.shadow(offset: v)
a.fromValue = NSValue(cgSize: s.shadowOffset)
anims.append(a)
}
if let v = targetState.shadowOpacity {
let a = MotionBasicAnimation.shadow(opacity: v)
let a = MotionCAAnimation.shadow(opacity: v)
a.fromValue = NSNumber(floatLiteral: Double(s.shadowOpacity))
anims.append(a)
}
if let v = targetState.shadowRadius {
let a = MotionBasicAnimation.shadow(radius: v)
let a = MotionCAAnimation.shadow(radius: v)
a.fromValue = NSNumber(floatLiteral: Double(s.shadowRadius))
anims.append(a)
}
if #available(iOS 9.0, *), let (stiffness, damping) = targetState.spring {
for i in 0..<anims.count where "cornerRadius" != anims[i].keyPath {
anims[i] = MotionCAAnimation.convert(basic: anims[i], stiffness: stiffness, damping: damping)
}
}
let g = Motion.animate(group: anims, duration: d)
g.fillMode = MotionAnimationFillModeToValue(mode: .forwards)
......
......@@ -447,267 +447,3 @@ extension MotionAnimation {
}
}
}
public enum MotionAnimationKeyPath: String {
case backgroundColor
case barTintColor
case borderColor
case borderWidth
case cornerRadius
case transform
case rotate = "transform.rotation"
case rotateX = "transform.rotation.x"
case rotateY = "transform.rotation.y"
case rotateZ = "transform.rotation.z"
case scale = "transform.scale"
case scaleX = "transform.scale.x"
case scaleY = "transform.scale.y"
case scaleZ = "transform.scale.z"
case translation = "transform.translation"
case translationX = "transform.translation.x"
case translationY = "transform.translation.y"
case translationZ = "transform.translation.z"
case position
case opacity
case zPosition
case width = "bounds.size.width"
case height = "bounds.size.height"
case size = "bounds.size"
case shadowPath
case shadowColor
case shadowOffset
case shadowOpacity
case shadowRadius
}
extension CABasicAnimation {
/**
A convenience initializer that takes a given MotionAnimationKeyPath.
- Parameter keyPath: An MotionAnimationKeyPath.
*/
public convenience init(keyPath: MotionAnimationKeyPath) {
self.init(keyPath: keyPath.rawValue)
}
}
public struct MotionBasicAnimation {
/**
Creates a CABasicAnimation for the backgroundColor key path.
- Parameter color: A UIColor.
- Returns: A CABasicAnimation.
*/
public static func background(color: UIColor) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .backgroundColor)
a.toValue = color.cgColor
return a
}
/**
Creates a CABasicAnimation for the barTintColor key path.
- Parameter color: A UIColor.
- Returns: A CABasicAnimation.
*/
public static func barTint(color: UIColor) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .barTintColor)
a.toValue = color.cgColor
return a
}
/**
Creates a CABasicAnimation for the borderColor key path.
- Parameter color: A UIColor.
- Returns: A CABasicAnimation.
*/
public static func border(color: UIColor) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .borderColor)
a.toValue = color.cgColor
return a
}
/**
Creates a CABasicAnimation for the borderWidth key path.
- Parameter width: A CGFloat.
- Returns: A CABasicAnimation.
*/
public static func border(width: CGFloat) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .borderWidth)
a.toValue = NSNumber(floatLiteral: Double(width))
return a
}
/**
Creates a CABasicAnimation for the cornerRadius key path.
- Parameter radius: A CGFloat.
- Returns: A CABasicAnimation.
*/
public static func corner(radius: CGFloat) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .cornerRadius)
a.toValue = NSNumber(floatLiteral: Double(radius))
return a
}
/**
Creates a CABasicAnimation for the transform key path.
- Parameter _ t: A CATransform3D object.
- Returns: A CABasicAnimation.
*/
public static func transform(_ t: CATransform3D) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .transform)
a.toValue = NSValue(caTransform3D: t)
return a
}
/**
Creates a CABasicAnimation for the transform.rotate.x key path.
- Parameter _ rotations: An optional CGFloat.
- Returns: A CABasicAnimation.
*/
public static func spinX(_ rotations: CGFloat) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .rotateX)
a.toValue = NSNumber(value: Double(CGFloat(Double.pi) * 2 * rotations))
return a
}
/**
Creates a CABasicAnimation for the transform.rotate.y key path.
- Parameter _ rotations: An optional CGFloat.
- Returns: A CABasicAnimation.
*/
public static func spinY(_ rotations: CGFloat) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .rotateY)
a.toValue = NSNumber(value: Double(CGFloat(Double.pi) * 2 * rotations))
return a
}
/**
Creates a CABasicAnimation for the transform.rotate.z key path.
- Parameter _ rotations: An optional CGFloat.
- Returns: A CABasicAnimation.
*/
public static func spinZ(_ rotations: CGFloat) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .rotateZ)
a.toValue = NSNumber(value: Double(CGFloat(Double.pi) * 2 * rotations))
return a
}
/**
Creates a CABasicAnimation for the position key path.
- Parameter _ point: A CGPoint.
- Returns: A CABasicAnimation.
*/
public static func position(_ point: CGPoint) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .position)
a.toValue = NSValue(cgPoint: point)
return a
}
/**
Creates a CABasicAnimation for the opacity key path.
- Parameter _ opacity: A Double.
- Returns: A CABasicAnimation.
*/
public static func fade(_ opacity: Double) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .opacity)
a.toValue = NSNumber(floatLiteral: opacity)
return a
}
/**
Creates a CABasicaAnimation for the zPosition key path.
- Parameter _ position: A CGFloat.
- Returns: A CABasicAnimation.
*/
public static func zPosition(_ position: CGFloat) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .zPosition)
a.toValue = NSNumber(value: Double(position))
return a
}
/**
Creates a CABasicaAnimation for the width key path.
- Parameter width: A CGFloat.
- Returns: A CABasicAnimation.
*/
public static func width(_ width: CGFloat) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .width)
a.toValue = NSNumber(floatLiteral: Double(width))
return a
}
/**
Creates a CABasicaAnimation for the height key path.
- Parameter height: A CGFloat.
- Returns: A CABasicAnimation.
*/
public static func height(_ height: CGFloat) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .height)
a.toValue = NSNumber(floatLiteral: Double(height))
return a
}
/**
Creates a CABasicaAnimation for the height key path.
- Parameter size: A CGSize.
- Returns: A CABasicAnimation.
*/
public static func size(_ size: CGSize) -> CABasicAnimation {
let a = CABasicAnimation(keyPath: .size)
a.toValue = NSValue(cgSize: size)
return a
}
/**
Creates a CABasicAnimation for the shadowPath key path.
- Parameter path: A CGPath.
- Returns: A CABasicAnimation.
*/
public static func shadow(path: CGPath) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .shadowPath)
animation.toValue = path
return animation
}
/**
Creates a CABasicAnimation for the shadowColor key path.
- Parameter color: A UIColor.
- Returns: A CABasicAnimation.
*/
public static func shadow(color: UIColor) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .shadowColor)
animation.toValue = color.cgColor
return animation
}
/**
Creates a CABasicAnimation for the shadowOffset key path.
- Parameter offset: A CGSize.
- Returns: A CABasicAnimation.
*/
public static func shadow(offset: CGSize) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .shadowOffset)
animation.toValue = NSValue(cgSize: offset)
return animation
}
/**
Creates a CABasicAnimation for the shadowOpacity key path.
- Parameter opacity: A Float.
- Returns: A CABasicAnimation.
*/
public static func shadow(opacity: Float) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .shadowOpacity)
animation.toValue = NSNumber(floatLiteral: Double(opacity))
return animation
}
/**
Creates a CABasicAnimation for the shadowRadius key path.
- Parameter radius: A CGFloat.
- Returns: A CABasicAnimation.
*/
public static func shadow(radius: CGFloat) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .shadowRadius)
animation.toValue = NSNumber(floatLiteral: Double(radius))
return animation
}
}
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