Commit 7a6f9cbd by Daniel Dahan

updated MotionAnimation and MotionTransition APIs

parent 06f6c0cd
......@@ -155,7 +155,7 @@ fileprivate extension CALayer {
return
}
var a = [CABasicAnimation]()
var anims = [CABasicAnimation]()
let tf: CAMediaTimingFunction = targetState.timingFunction ?? CAMediaTimingFunction.from(mediaTimingFunctionType: timingFunction)
let d: TimeInterval = targetState.duration ?? duration
//
......@@ -183,92 +183,68 @@ fileprivate extension CALayer {
// }
//
if let v = targetState.backgroundColor {
let anim = MotionBasicAnimation.background(color: UIColor(cgColor: v))
anim.fromValue = s.backgroundColor
a.append(anim)
let a = MotionBasicAnimation.background(color: UIColor(cgColor: v))
a.fromValue = s.backgroundColor
anims.append(a)
}
if let v = targetState.borderColor {
let anim = MotionBasicAnimation.border(color: UIColor(cgColor: v))
anim.fromValue = s.borderColor
a.append(anim)
let a = MotionBasicAnimation.border(color: UIColor(cgColor: v))
a.fromValue = s.borderColor
anims.append(a)
}
if let v = targetState.borderWidth {
let anim = MotionBasicAnimation.border(width: v)
anim.fromValue = NSNumber(floatLiteral: Double(s.borderWidth))
a.append(anim)
let a = MotionBasicAnimation.border(width: v)
a.fromValue = NSNumber(floatLiteral: Double(s.borderWidth))
anims.append(a)
}
if let v = targetState.cornerRadius {
let anim = MotionBasicAnimation.corner(radius: v)
anim.fromValue = NSNumber(floatLiteral: Double(s.cornerRadius))
a.append(anim)
let a = MotionBasicAnimation.corner(radius: v)
a.fromValue = NSNumber(floatLiteral: Double(s.cornerRadius))
anims.append(a)
}
if let v = targetState.transform {
let anim = MotionBasicAnimation.transform(transform: v)
anim.fromValue = NSValue(caTransform3D: s.transform)
a.append(anim)
let a = MotionBasicAnimation.transform(v)
a.fromValue = NSValue(caTransform3D: s.transform)
anims.append(a)
}
if let v = targetState.spin {
var anim = MotionBasicAnimation.spinX(rotates: v.0)
anim.fromValue = 0
a.append(anim)
var a = MotionBasicAnimation.spinX(v.0)
a.fromValue = NSNumber(floatLiteral: 0)
anims.append(a)
anim = MotionBasicAnimation.spinY(rotates: v.1)
anim.fromValue = 0
a.append(anim)
a = MotionBasicAnimation.spinY(v.1)
a.fromValue = NSNumber(floatLiteral: 0)
anims.append(a)
anim = MotionBasicAnimation.spinZ(rotates: v.2)
anim.fromValue = 0
a.append(anim)
a = MotionBasicAnimation.spinZ(v.2)
a.fromValue = NSNumber(floatLiteral: 0)
anims.append(a)
}
if let v = targetState.position {
let a = MotionBasicAnimation.position(v)
a.fromValue = NSValue(cgPoint: s.position)
anims.append(a)
}
if let v = targetState.opacity {
let a = MotionBasicAnimation.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)
a.fromValue = s.value(forKeyPath: MotionAnimationKeyPath.zPosition.rawValue) ?? NSNumber(floatLiteral: 0)
anims.append(a)
}
//
// case let .scale(to):
// a.append(MotionBasicAnimation.scale(to: to))
//
// case let .scaleX(to):
// a.append(MotionBasicAnimation.scaleX(to: to))
//
// case let .scaleY(to):
// a.append(MotionBasicAnimation.scaleY(to: to))
//
// case let .scaleZ(to):
// a.append(MotionBasicAnimation.scaleZ(to: to))
//
// case let .translate(x, y):
// a.append(MotionBasicAnimation.translate(to: CGPoint(x: x, y: y)))
//
// case let .translateX(to):
// a.append(MotionBasicAnimation.translateX(to: to))
//
// case let .translateY(to):
// a.append(MotionBasicAnimation.translateY(to: to))
//
// case let .translateZ(to):
// a.append(MotionBasicAnimation.translateZ(to: to))
//
// case .x(_), .y(_), .point(_, _):
// let position = MotionBasicAnimation.position(to: CGPoint(x: px, y: py))
// a.append(position)
//
// case let .position(x, y):
// a.append(MotionBasicAnimation.position(to: CGPoint(x: x, y: y)))
//
// case let .fade(opacity):
// let fade = MotionBasicAnimation.fade(to: opacity)
// fade.fromValue = s.value(forKey: MotionAnimationKeyPath.opacity.rawValue) ?? NSNumber(floatLiteral: 1)
// a.append(fade)
//
// case let .zPosition(position):
// let zPosition = MotionBasicAnimation.zPosition(position)
// zPosition.fromValue = s.value(forKey: MotionAnimationKeyPath.zPosition.rawValue) ?? NSNumber(value: 0)
// a.append(zPosition)
//
// case .width(_), .height(_), .size(_, _):
// a.append(MotionBasicAnimation.size(CGSize(width: w, height: h)))
//
......@@ -318,7 +294,7 @@ fileprivate extension CALayer {
// }
// }
//
let g = Motion.animate(group: a, duration: d)
let g = Motion.animate(group: anims, duration: d)
g.fillMode = MotionAnimationFillModeToValue(mode: .forwards)
g.isRemovedOnCompletion = false
g.timingFunction = tf
......
......@@ -42,7 +42,7 @@ public struct MotionAnimationState {
public var spin: (CGFloat, CGFloat, CGFloat)?
/// A reference to the opacity.
public var opacity: Float?
public var opacity: Double?
/// A reference to the cornerRadius.
public var cornerRadius: CGFloat?
......@@ -113,18 +113,18 @@ public struct MotionAnimationState {
extension MotionAnimationState {
/**
Adds a MotionAnimation to the current state.
- Parameter _ animation: A MotionAnimation.
- Parameter _ element: A MotionAnimation.
*/
public mutating func append(_ animation: MotionAnimation) {
animation.apply(&self)
public mutating func append(_ element: MotionAnimation) {
element.apply(&self)
}
/**
Adds an Array of MotionAnimations to the current state.
- Parameter contentsOf animations: An Array of MotionAnimations.
- Parameter contentsOf elements: An Array of MotionAnimations.
*/
public mutating func append(contentsOf animations: [MotionAnimation]) {
for v in animations {
public mutating func append(contentsOf elements: [MotionAnimation]) {
for v in elements {
v.apply(&self)
}
}
......
......@@ -191,11 +191,11 @@ extension MotionTransition {
/**
Animates the view's current x & y scale to the given scale value.
- Parameter to scale: A CGFloat.
- Parameter _ xy: A CGFloat.
- Returns: A MotionTransition.
*/
public static func scale(to scale: CGFloat) -> MotionTransition {
return .scale(x: scale, y: scale)
public static func scale(_ xy: CGFloat) -> MotionTransition {
return .scale(x: xy, y: xy)
}
/**
......@@ -215,20 +215,20 @@ extension MotionTransition {
/**
Animates the view's current translation to the given
point value (x & y), and a z value.
- Parameter to point: A CGPoint.
- Parameter _ point: A CGPoint.
- Parameter z: A CGFloat, default is 0.
- Returns: A MotionTransition.
*/
public static func translate(to point: CGPoint, z: CGFloat = 0) -> MotionTransition {
public static func translate(_ point: CGPoint, z: CGFloat = 0) -> MotionTransition {
return .translate(x: point.x, y: point.y, z: z)
}
/**
Animates the view's current position to the given point.
- Parameter to point: A CGPoint.
- Parameter _ point: A CGPoint.
- Returns: A MotionTransition.
*/
public static func position(to point: CGPoint) -> MotionTransition {
public static func position(_ point: CGPoint) -> MotionTransition {
return MotionTransition {
$0.position = point
}
......@@ -239,17 +239,18 @@ extension MotionTransition {
$0.nonFade = true
}
/// Fades the view in during a transition.
public static var fadeIn = MotionTransition.fade(1)
/// Fades the view out during a transition.
public static var fade = MotionTransition {
$0.opacity = 0
}
public static var fadeOut = MotionTransition.fade(0)
/**
Animates the view's current opacity to the given one.
- Parameter to opacity: A Float value.
- Parameter to opacity: A Double value.
- Returns: A MotionTransition.
*/
public static func fade(to opacity: Float) -> MotionTransition {
public static func fade(_ opacity: Double) -> MotionTransition {
return MotionTransition {
$0.opacity = opacity
}
......
......@@ -58,7 +58,7 @@ public struct MotionTransitionState {
public var transform: CATransform3D?
/// A reference to the opacity.
public var opacity: Float?
public var opacity: Double?
/// A reference to the cornerRadius.
public var cornerRadius: CGFloat?
......@@ -162,18 +162,18 @@ public struct MotionTransitionState {
extension MotionTransitionState {
/**
Adds a MotionTransition to the current state.
- Parameter _ transition: A MotionTransition.
- Parameter _ element: A MotionTransition.
*/
public mutating func append(_ transition: MotionTransition) {
transition.apply(&self)
public mutating func append(_ element: MotionTransition) {
element.apply(&self)
}
/**
Adds an Array of MotionTransitions to the current state.
- Parameter contentsOf transitions: An Array of MotionTransitions.
- Parameter contentsOf elements: An Array of MotionTransitions.
*/
public mutating func append(contentsOf transitions: [MotionTransition]) {
for v in transitions {
public mutating func append(contentsOf elements: [MotionTransition]) {
for v in elements {
v.apply(&self)
}
}
......
......@@ -261,37 +261,37 @@ class TransitionPreprocessor: MotionPreprocessor {
switch defaultAnimation {
case .push(let direction):
context[tv]!.append(contentsOf: [.translate(to: shift(direction: direction, isAppearing: true)),
context[tv]!.append(contentsOf: [.translate(shift(direction: direction, isAppearing: true)),
.shadow(opacity: 0),
.beginWith(transitions: shadowState),
.timingFunction(.deceleration)])
context[fv]!.append(contentsOf: [.translate(to: shift(direction: direction, isAppearing: false) / 3),
context[fv]!.append(contentsOf: [.translate(shift(direction: direction, isAppearing: false) / 3),
.overlay(color: .black, opacity: 0.1),
.timingFunction(.deceleration)])
case .pull(let direction):
m.insertToViewFirst = true
context[fv]!.append(contentsOf: [.translate(to: shift(direction: direction, isAppearing: false)),
context[fv]!.append(contentsOf: [.translate(shift(direction: direction, isAppearing: false)),
.shadow(opacity: 0),
.beginWith(transitions: shadowState)])
context[tv]!.append(contentsOf: [.translate(to: shift(direction: direction, isAppearing: true) / 3),
context[tv]!.append(contentsOf: [.translate(shift(direction: direction, isAppearing: true) / 3),
.overlay(color: .black, opacity: 0.1)])
case .slide(let direction):
context[fv]!.append(contentsOf: [.translate(to: shift(direction: direction, isAppearing: false))])
context[fv]!.append(contentsOf: [.translate(shift(direction: direction, isAppearing: false))])
context[tv]!.append(contentsOf: [.translate(to: shift(direction: direction, isAppearing: true))])
context[tv]!.append(contentsOf: [.translate(shift(direction: direction, isAppearing: true))])
case .zoomSlide(let direction):
context[fv]!.append(contentsOf: [.translate(to: shift(direction: direction, isAppearing: false)), .scale(to: 0.8)])
context[fv]!.append(contentsOf: [.translate(shift(direction: direction, isAppearing: false)), .scale(0.8)])
context[tv]!.append(contentsOf: [.translate(to: shift(direction: direction, isAppearing: true)), .scale(to: 0.8)])
context[tv]!.append(contentsOf: [.translate(shift(direction: direction, isAppearing: true)), .scale(0.8)])
case .cover(let direction):
context[tv]!.append(contentsOf: [.translate(to: shift(direction: direction, isAppearing: true)),
context[tv]!.append(contentsOf: [.translate(shift(direction: direction, isAppearing: true)),
.shadow(opacity: 0),
.beginWith(transitions: shadowState),
.timingFunction(.deceleration)])
......@@ -302,43 +302,43 @@ class TransitionPreprocessor: MotionPreprocessor {
case .uncover(let direction):
m.insertToViewFirst = true
context[fv]!.append(contentsOf: [.translate(to: shift(direction: direction, isAppearing: false)),
context[fv]!.append(contentsOf: [.translate(shift(direction: direction, isAppearing: false)),
.shadow(opacity: 0),
.beginWith(transitions: shadowState)])
context[tv]!.append(contentsOf: [.overlay(color: .black, opacity: 0.1)])
case .pageIn(let direction):
context[tv]!.append(contentsOf: [.translate(to: shift(direction: direction, isAppearing: true)),
context[tv]!.append(contentsOf: [.translate(shift(direction: direction, isAppearing: true)),
.shadow(opacity: 0),
.beginWith(transitions: shadowState),
.timingFunction(.deceleration)])
context[fv]!.append(contentsOf: [.scale(to: 0.7),
context[fv]!.append(contentsOf: [.scale(0.7),
.overlay(color: .black, opacity: 0.1),
.timingFunction(.deceleration)])
case .pageOut(let direction):
m.insertToViewFirst = true
context[fv]!.append(contentsOf: [.translate(to: shift(direction: direction, isAppearing: false)),
context[fv]!.append(contentsOf: [.translate(shift(direction: direction, isAppearing: false)),
.shadow(opacity: 0),
.beginWith(transitions: shadowState)])
context[tv]!.append(contentsOf: [.scale(to: 0.7),
context[tv]!.append(contentsOf: [.scale(0.7),
.overlay(color: .black, opacity: 0.1)])
case .fade:
// TODO: clean up this. overFullScreen logic shouldn't be here
if !(fromOverFullScreen && !isPresenting) {
context[tv] = [.fade]
context[tv] = [.fadeOut]
}
#if os(tvOS)
context[fromView] = [.fade]
#else
if (!isPresenting && toOverFullScreen) || !fv.isOpaque || (fv.backgroundColor?.alphaComponent ?? 1) < 1 {
context[fv] = [.fade]
context[fv] = [.fadeOut]
}
#endif
......@@ -347,12 +347,12 @@ class TransitionPreprocessor: MotionPreprocessor {
case .zoom:
m.insertToViewFirst = true
context[fv]!.append(contentsOf: [.scale(to: 1.3), .fade])
context[tv]!.append(contentsOf: [.scale(to: 0.7)])
context[fv]!.append(contentsOf: [.scale(1.3), .fadeOut])
context[tv]!.append(contentsOf: [.scale(0.7)])
case .zoomOut:
context[tv]!.append(contentsOf: [.scale(to: 1.3), .fade])
context[fv]!.append(contentsOf: [.scale(to: 0.7)])
context[tv]!.append(contentsOf: [.scale(1.3), .fadeOut])
context[fv]!.append(contentsOf: [.scale(0.7)])
default:
fatalError("Not implemented")
......
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