Commit a8b1aa0e by Daniel Dahan

updated Motion to handle depth animations

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