Commit a8b1aa0e by Daniel Dahan

updated Motion to handle depth animations

parent 7a6f9cbd
......@@ -147,7 +147,6 @@ fileprivate extension CALayer {
- Parameter completion: An optional completion block.
*/
func animate(delay: TimeInterval, duration: TimeInterval, timingFunction: CAMediaTimingFunctionType, animations: [MotionAnimation], completion: (() -> Void)? = nil) {
let targetState = MotionAnimationState(animations: animations)
Motion.delay(targetState.delay) { [weak self] in
......@@ -156,32 +155,10 @@ fileprivate extension CALayer {
}
var anims = [CABasicAnimation]()
let tf: CAMediaTimingFunction = targetState.timingFunction ?? CAMediaTimingFunction.from(mediaTimingFunctionType: timingFunction)
let d: TimeInterval = targetState.duration ?? duration
//
// var w: CGFloat = s.bounds.width
// var h: CGFloat = s.bounds.height
//
//
// var px: CGFloat = s.position.x
// var py: CGFloat = s.position.y
//
// for v in animations {
// switch v {
// case let .x(x):
// px = x + w / 2
//
// case let .y(y):
// py = y + h / 2
//
// case let .point(x, y):
// px = x + w / 2
// py = y + h / 2
//
// default:break
// }
// }
//
if let v = targetState.backgroundColor {
let a = MotionBasicAnimation.background(color: UIColor(cgColor: v))
a.fromValue = s.backgroundColor
......@@ -244,56 +221,42 @@ fileprivate extension CALayer {
anims.append(a)
}
// case .width(_), .height(_), .size(_, _):
// a.append(MotionBasicAnimation.size(CGSize(width: w, height: h)))
//
// case let .shadowPath(path):
// let shadowPath = MotionBasicAnimation.shadow(path: path)
// shadowPath.fromValue = s.shadowPath
// a.append(shadowPath)
//
// case let .shadowColor(color):
// a.append(MotionBasicAnimation.shadow(color: color))
//
// case let .shadowOffset(offset):
// let shadowOffset = MotionBasicAnimation.shadow(offset: offset)
// shadowOffset.fromValue = s.shadowOffset
// a.append(shadowOffset)
//
// case let .shadowOpacity(opacity):
// let shadowOpacity = MotionBasicAnimation.shadow(opacity: opacity)
// shadowOpacity.fromValue = s.shadowOpacity
// a.append(shadowOpacity)
//
// case let .shadowRadius(radius):
// let shadowRadius = MotionBasicAnimation.shadow(radius: radius)
// shadowRadius.fromValue = s.shadowRadius
// a.append(shadowRadius)
//
// case let .depth(offset, opacity, radius):
// if let path = s.shadowPath {
// let shadowPath = MotionBasicAnimation.shadow(path: path)
// shadowPath.fromValue = s.shadowPath
// a.append(shadowPath)
// }
//
// let shadowOffset = MotionBasicAnimation.shadow(offset: offset)
// shadowOffset.fromValue = s.shadowOffset
// a.append(shadowOffset)
//
// let shadowOpacity = MotionBasicAnimation.shadow(opacity: opacity)
// shadowOpacity.fromValue = s.shadowOpacity
// a.append(shadowOpacity)
//
// let shadowRadius = MotionBasicAnimation.shadow(radius: radius)
// shadowRadius.fromValue = s.shadowRadius
// a.append(shadowRadius)
//
// default:break
// }
// }
//
if let v = targetState.size {
let a = MotionBasicAnimation.size(v)
a.fromValue = NSValue(cgSize: s.bounds.size)
anims.append(a)
}
if let v = targetState.shadowPath {
let a = MotionBasicAnimation.shadow(path: v)
a.fromValue = s.shadowPath
anims.append(a)
}
if let v = targetState.shadowColor {
let a = MotionBasicAnimation.shadow(color: UIColor(cgColor: v))
a.fromValue = s.shadowColor
anims.append(a)
}
if let v = targetState.shadowOffset {
let a = MotionBasicAnimation.shadow(offset: v)
a.fromValue = NSValue(cgSize: s.shadowOffset)
anims.append(a)
}
if let v = targetState.shadowOpacity {
let a = MotionBasicAnimation.shadow(opacity: v)
a.fromValue = NSNumber(floatLiteral: Double(s.shadowOpacity))
anims.append(a)
}
if let v = targetState.shadowRadius {
let a = MotionBasicAnimation.shadow(radius: v)
a.fromValue = NSNumber(floatLiteral: Double(s.shadowRadius))
anims.append(a)
}
let g = Motion.animate(group: anims, duration: d)
g.fillMode = MotionAnimationFillModeToValue(mode: .forwards)
g.isRemovedOnCompletion = false
......
......@@ -28,7 +28,7 @@
import UIKit
public final class MotionAnimation {
public class MotionAnimation {
/// A reference to the callback that applies the MotionAnimationState.
internal let apply: (inout MotionAnimationState) -> Void
......@@ -251,7 +251,7 @@ extension MotionAnimation {
/**
Animates the view's current opacity to the given one.
- Parameter _ opacity: A Double value.
- Parameter _ opacity: A Double.
- Returns: A MotionAnimation.
*/
public static func fade(_ opacity: Double) -> MotionAnimation {
......@@ -317,12 +317,12 @@ extension MotionAnimation {
/**
Animates the view's current shadow opacity to the given one.
- Parameter opacity: A CGFloat.
- Parameter opacity: A Float.
- Returns: A MotionAnimation.
*/
public static func shadow(opacity: CGFloat) -> MotionAnimation {
public static func shadow(opacity: Float) -> MotionAnimation {
return MotionAnimation {
$0.shadowOpacity = Float(opacity)
$0.shadowOpacity = opacity
}
}
......@@ -338,6 +338,28 @@ extension MotionAnimation {
}
/**
Animates the views shadow offset, opacity, and radius.
- Parameter offset: A CGSize.
- Parameter opacity: A Float.
- Parameter radius: A CGFloat.
*/
public static func depth(offset: CGSize, opacity: Float, radius: CGFloat) -> MotionAnimation {
return MotionAnimation {
$0.shadowOffset = offset
$0.shadowOpacity = opacity
$0.shadowRadius = radius
}
}
/**
Animates the views shadow offset, opacity, and radius.
- Parameter _ depth: A tuple (CGSize, FLoat, CGFloat).
*/
public static func depth(_ depth: (CGSize, Float, CGFloat)) -> MotionAnimation {
return .depth(offset: depth.0, opacity: depth.1, radius: depth.2)
}
/**
Animates the view's contents rect to the given one.
- Parameter rect: A CGRect.
- Returns: A MotionAnimation.
......@@ -424,20 +446,6 @@ extension MotionAnimation {
$0.arc = intensity
}
}
/**
Animates subviews with an increasing delay between each animation.
- Parameter delta: A TimeInterval.
- Parameter direction: A CascadeDirection.
- Parameter animationDelayUntilMatchedViews: A boolean indicating whether
or not to delay the subview animation until all have started.
- Returns: A MotionAnimation.
*/
public static func cascade(delta: TimeInterval = 0.02, direction: CascadeDirection = .topToBottom, animationDelayUntilMatchedViews: Bool = false) -> MotionAnimation {
return MotionAnimation {
$0.cascade = (delta, direction, animationDelayUntilMatchedViews)
}
}
}
public enum MotionAnimationKeyPath: String {
......@@ -599,9 +607,9 @@ public struct MotionBasicAnimation {
- Returns: A CABasicAnimation.
*/
public static func fade(_ opacity: Double) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .opacity)
animation.toValue = NSNumber(floatLiteral: opacity)
return animation
let a = CABasicAnimation(keyPath: .opacity)
a.toValue = NSNumber(floatLiteral: opacity)
return a
}
/**
......@@ -610,9 +618,9 @@ public struct MotionBasicAnimation {
- Returns: A CABasicAnimation.
*/
public static func zPosition(_ position: CGFloat) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .zPosition)
animation.toValue = NSNumber(value: Double(position))
return animation
let a = CABasicAnimation(keyPath: .zPosition)
a.toValue = NSNumber(value: Double(position))
return a
}
/**
......@@ -621,9 +629,9 @@ public struct MotionBasicAnimation {
- Returns: A CABasicAnimation.
*/
public static func width(_ width: CGFloat) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .width)
animation.toValue = NSNumber(floatLiteral: Double(width))
return animation
let a = CABasicAnimation(keyPath: .width)
a.toValue = NSNumber(floatLiteral: Double(width))
return a
}
/**
......@@ -632,9 +640,9 @@ public struct MotionBasicAnimation {
- Returns: A CABasicAnimation.
*/
public static func height(_ height: CGFloat) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .height)
animation.toValue = NSNumber(floatLiteral: Double(height))
return animation
let a = CABasicAnimation(keyPath: .height)
a.toValue = NSNumber(floatLiteral: Double(height))
return a
}
/**
......@@ -643,9 +651,9 @@ public struct MotionBasicAnimation {
- Returns: A CABasicAnimation.
*/
public static func size(_ size: CGSize) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .size)
animation.toValue = NSValue(cgSize: size)
return animation
let a = CABasicAnimation(keyPath: .size)
a.toValue = NSValue(cgSize: size)
return a
}
/**
......@@ -672,7 +680,7 @@ public struct MotionBasicAnimation {
/**
Creates a CABasicAnimation for the shadowOffset key path.
- Parameter offset: CGSize.
- Parameter offset: A CGSize.
- Returns: A CABasicAnimation.
*/
public static func shadow(offset: CGSize) -> CABasicAnimation {
......@@ -683,7 +691,7 @@ public struct MotionBasicAnimation {
/**
Creates a CABasicAnimation for the shadowOpacity key path.
- Parameter opacity: Float.
- Parameter opacity: A Float.
- Returns: A CABasicAnimation.
*/
public static func shadow(opacity: Float) -> CABasicAnimation {
......@@ -694,7 +702,7 @@ public struct MotionBasicAnimation {
/**
Creates a CABasicAnimation for the shadowRadius key path.
- Parameter radius: CGFloat.
- Parameter radius: A CGFloat.
- Returns: A CABasicAnimation.
*/
public static func shadow(radius: CGFloat) -> CABasicAnimation {
......
......@@ -95,9 +95,6 @@ public struct MotionAnimationState {
/// The arc curve value.
public var arc: CGFloat?
/// The cascading animation settings.
public var cascade: (TimeInterval, CascadeDirection, Bool)?
/// Custom target states.
public var custom: [String: Any]?
......
......@@ -28,7 +28,7 @@
import UIKit
public final class MotionTransition {
public class MotionTransition {
/// A reference to the callback that applies the MotionTransitionState.
internal let apply: (inout MotionTransitionState) -> Void
......@@ -247,7 +247,7 @@ extension MotionTransition {
/**
Animates the view's current opacity to the given one.
- Parameter to opacity: A Double value.
- Parameter to opacity: A Double.
- Returns: A MotionTransition.
*/
public static func fade(_ opacity: Double) -> MotionTransition {
......@@ -313,12 +313,12 @@ extension MotionTransition {
/**
Animates the view's current shadow opacity to the given one.
- Parameter opacity: A CGFloat.
- Parameter opacity: A Float.
- Returns: A MotionTransition.
*/
public static func shadow(opacity: CGFloat) -> MotionTransition {
public static func shadow(opacity: Float) -> MotionTransition {
return MotionTransition {
$0.shadowOpacity = Float(opacity)
$0.shadowOpacity = opacity
}
}
......
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