Commit 5e35f736 by Daniel Dahan

development: added animation rotation to MotionTransition

parent 1cfbad4a
...@@ -63,18 +63,18 @@ public enum MotionAnimation { ...@@ -63,18 +63,18 @@ public enum MotionAnimation {
case backgroundColor(UIColor) case backgroundColor(UIColor)
case corners(CGFloat) case corners(CGFloat)
case transform(CATransform3D) case transform(CATransform3D)
case rotate(CGFloat) case rotate(Double)
case rotateX(CGFloat) case rotateX(Double)
case rotateY(CGFloat) case rotateY(Double)
case rotateZ(CGFloat) case rotateZ(Double)
case spin(CGFloat) case spin(Double)
case spinX(CGFloat) case spinX(Double)
case spinY(CGFloat) case spinY(Double)
case spinZ(CGFloat) case spinZ(Double)
case scale(CGFloat) case scale(Double)
case scaleX(CGFloat) case scaleX(Double)
case scaleY(CGFloat) case scaleY(Double)
case scaleZ(CGFloat) case scaleZ(Double)
case translate(x: CGFloat, y: CGFloat) case translate(x: CGFloat, y: CGFloat)
case translateX(CGFloat) case translateX(CGFloat)
case translateY(CGFloat) case translateY(CGFloat)
...@@ -84,7 +84,7 @@ public enum MotionAnimation { ...@@ -84,7 +84,7 @@ public enum MotionAnimation {
case point(x: CGFloat, y: CGFloat) case point(x: CGFloat, y: CGFloat)
case position(x: CGFloat, y: CGFloat) case position(x: CGFloat, y: CGFloat)
case shadow(path: CGPath) case shadow(path: CGPath)
case fade(CGFloat) case fade(Double)
case zPosition(Int) case zPosition(Int)
case width(CGFloat) case width(CGFloat)
case height(CGFloat) case height(CGFloat)
...@@ -234,8 +234,6 @@ extension CALayer { ...@@ -234,8 +234,6 @@ extension CALayer {
a.append(Motion.transform(transform: transform)) a.append(Motion.transform(transform: transform))
case let .rotate(angle): case let .rotate(angle):
let rotate = Motion.rotate(angle: angle) let rotate = Motion.rotate(angle: angle)
let radians = CGFloat(atan2f(Float(s.affineTransform().b), Float(s.affineTransform().a)))
rotate.fromValue = radians * 180 / CGFloat(M_PI)
a.append(rotate) a.append(rotate)
case let .rotateX(angle): case let .rotateX(angle):
a.append(Motion.rotateX(angle: angle)) a.append(Motion.rotateX(angle: angle))
...@@ -370,98 +368,98 @@ extension Motion { ...@@ -370,98 +368,98 @@ extension Motion {
/** /**
Creates a CABasicAnimation for the transform.rotation key path. Creates a CABasicAnimation for the transform.rotation key path.
- Parameter angle: An optional CGFloat. - Parameter angle: An optional Double.
- Returns: A CABasicAnimation. - Returns: A CABasicAnimation.
*/ */
public static func rotate(angle: CGFloat) -> CABasicAnimation { public static func rotate(angle: Double) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .rotation) let animation = CABasicAnimation(keyPath: .rotation)
animation.toValue = (CGFloat(M_PI) * angle / 180) as NSNumber animation.toValue = NSNumber(floatLiteral: M_PI * angle / 180)
return animation return animation
} }
/** /**
Creates a CABasicAnimation for the transform.rotation.x key path. Creates a CABasicAnimation for the transform.rotation.x key path.
- Parameter angle: An optional CGFloat. - Parameter angle: An optional Double.
- Returns: A CABasicAnimation. - Returns: A CABasicAnimation.
*/ */
public static func rotateX(angle: CGFloat) -> CABasicAnimation { public static func rotateX(angle: Double) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .rotationX) let animation = CABasicAnimation(keyPath: .rotationX)
animation.toValue = (CGFloat(M_PI) * angle / 180) as NSNumber animation.toValue = NSNumber(floatLiteral: M_PI * angle / 180)
return animation return animation
} }
/** /**
Creates a CABasicAnimation for the transform.rotation.y key path. Creates a CABasicAnimation for the transform.rotation.y key path.
- Parameter angle: An optional CGFloat. - Parameter angle: An optional Double.
- Returns: A CABasicAnimation. - Returns: A CABasicAnimation.
*/ */
public static func rotateY(angle: CGFloat) -> CABasicAnimation { public static func rotateY(angle: Double) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .rotationY) let animation = CABasicAnimation(keyPath: .rotationY)
animation.toValue = (CGFloat(M_PI) * angle / 180) as NSNumber animation.toValue = NSNumber(floatLiteral: M_PI * angle / 180)
return animation return animation
} }
/** /**
Creates a CABasicAnimation for the transform.rotation.z key path. Creates a CABasicAnimation for the transform.rotation.z key path.
- Parameter angle: An optional CGFloat. - Parameter angle: An optional Double.
- Returns: A CABasicAnimation. - Returns: A CABasicAnimation.
*/ */
public static func rotateZ(angle: CGFloat) -> CABasicAnimation { public static func rotateZ(angle: Double) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .rotationZ) let animation = CABasicAnimation(keyPath: .rotationZ)
animation.toValue = (CGFloat(M_PI) * angle / 180) as NSNumber animation.toValue = NSNumber(floatLiteral: M_PI * angle / 180)
return animation return animation
} }
/** /**
Creates a CABasicAnimation for the transform.rotation key path. Creates a CABasicAnimation for the transform.rotation key path.
- Parameter rotations: An optional CGFloat. - Parameter rotations: An optional Double.
- Returns: A CABasicAnimation. - Returns: A CABasicAnimation.
*/ */
public static func spin(rotations: CGFloat) -> CABasicAnimation { public static func spin(rotations: Double) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .rotation) let animation = CABasicAnimation(keyPath: .rotation)
animation.toValue = (CGFloat(M_PI * 2) * rotations) as NSNumber animation.toValue = NSNumber(floatLiteral: M_PI * 2 * rotations)
return animation return animation
} }
/** /**
Creates a CABasicAnimation for the transform.rotation.x key path. Creates a CABasicAnimation for the transform.rotation.x key path.
- Parameter rotations: An optional CGFloat. - Parameter rotations: An optional Double.
- Returns: A CABasicAnimation. - Returns: A CABasicAnimation.
*/ */
public static func spinX(rotations: CGFloat) -> CABasicAnimation { public static func spinX(rotations: Double) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .rotationX) let animation = CABasicAnimation(keyPath: .rotationX)
animation.toValue = (CGFloat(M_PI * 2) * rotations) as NSNumber animation.toValue = NSNumber(floatLiteral: M_PI * 2 * rotations)
return animation return animation
} }
/** /**
Creates a CABasicAnimation for the transform.rotation.y key path. Creates a CABasicAnimation for the transform.rotation.y key path.
- Parameter rotations: An optional CGFloat. - Parameter rotations: An optional Double.
- Returns: A CABasicAnimation. - Returns: A CABasicAnimation.
*/ */
public static func spinY(rotations: CGFloat) -> CABasicAnimation { public static func spinY(rotations: Double) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .rotationY) let animation = CABasicAnimation(keyPath: .rotationY)
animation.toValue = (CGFloat(M_PI * 2) * rotations) as NSNumber animation.toValue = NSNumber(floatLiteral: M_PI * 2 * rotations)
return animation return animation
} }
/** /**
Creates a CABasicAnimation for the transform.rotation.z key path. Creates a CABasicAnimation for the transform.rotation.z key path.
- Parameter rotations: An optional CGFloat. - Parameter rotations: An optional Double.
- Returns: A CABasicAnimation. - Returns: A CABasicAnimation.
*/ */
public static func spinZ(rotations: CGFloat) -> CABasicAnimation { public static func spinZ(rotations: Double) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .rotationZ) let animation = CABasicAnimation(keyPath: .rotationZ)
animation.toValue = (CGFloat(M_PI * 2) * rotations) as NSNumber animation.toValue = NSNumber(floatLiteral: M_PI * 2 * rotations)
return animation return animation
} }
/** /**
Creates a CABasicAnimation for the transform.scale key path. Creates a CABasicAnimation for the transform.scale key path.
- Parameter to scale: A CGFloat. - Parameter to scale: A Double.
- Returns: A CABasicAnimation. - Returns: A CABasicAnimation.
*/ */
public static func scale(to scale: CGFloat) -> CABasicAnimation { public static func scale(to scale: Double) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .scale) let animation = CABasicAnimation(keyPath: .scale)
animation.toValue = scale as NSNumber animation.toValue = scale as NSNumber
return animation return animation
...@@ -469,10 +467,10 @@ extension Motion { ...@@ -469,10 +467,10 @@ extension Motion {
/** /**
Creates a CABasicAnimation for the transform.scale.x key path. Creates a CABasicAnimation for the transform.scale.x key path.
- Parameter to scale: A CGFloat. - Parameter to scale: A Double.
- Returns: A CABasicAnimation. - Returns: A CABasicAnimation.
*/ */
public static func scaleX(to scale: CGFloat) -> CABasicAnimation { public static func scaleX(to scale: Double) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .scaleX) let animation = CABasicAnimation(keyPath: .scaleX)
animation.toValue = scale as NSNumber animation.toValue = scale as NSNumber
return animation return animation
...@@ -480,10 +478,10 @@ extension Motion { ...@@ -480,10 +478,10 @@ extension Motion {
/** /**
Creates a CABasicAnimation for the transform.scale.y key path. Creates a CABasicAnimation for the transform.scale.y key path.
- Parameter to scale: A CGFloat. - Parameter to scale: A Double.
- Returns: A CABasicAnimation. - Returns: A CABasicAnimation.
*/ */
public static func scaleY(to scale: CGFloat) -> CABasicAnimation { public static func scaleY(to scale: Double) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .scaleY) let animation = CABasicAnimation(keyPath: .scaleY)
animation.toValue = scale as NSNumber animation.toValue = scale as NSNumber
return animation return animation
...@@ -491,10 +489,10 @@ extension Motion { ...@@ -491,10 +489,10 @@ extension Motion {
/** /**
Creates a CABasicAnimation for the transform.scale.z key path. Creates a CABasicAnimation for the transform.scale.z key path.
- Parameter to scale: A CGFloat. - Parameter to scale: A Double.
- Returns: A CABasicAnimation. - Returns: A CABasicAnimation.
*/ */
public static func scaleZ(to scale: CGFloat) -> CABasicAnimation { public static func scaleZ(to scale: Double) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .scaleZ) let animation = CABasicAnimation(keyPath: .scaleZ)
animation.toValue = scale as NSNumber animation.toValue = scale as NSNumber
return animation return animation
...@@ -580,12 +578,12 @@ extension Motion { ...@@ -580,12 +578,12 @@ extension Motion {
/** /**
Creates a CABasicAnimation for the opacity key path. Creates a CABasicAnimation for the opacity key path.
- Parameter opacity: A CGFloat. - Parameter opacity: A Double.
- Returns: A CABasicAnimation. - Returns: A CABasicAnimation.
*/ */
public static func fade(opacity: CGFloat) -> CABasicAnimation { public static func fade(opacity: Double) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: .opacity) let animation = CABasicAnimation(keyPath: .opacity)
animation.toValue = NSNumber(floatLiteral: Double(opacity)) animation.toValue = NSNumber(floatLiteral: opacity)
return animation return animation
} }
......
...@@ -42,6 +42,11 @@ fileprivate struct MotionTransitionItemController { ...@@ -42,6 +42,11 @@ fileprivate struct MotionTransitionItemController {
fileprivate var delegate: MotionTransitionDelegate fileprivate var delegate: MotionTransitionDelegate
} }
fileprivate func getRotationInDegrees(view: UIView) -> Double {
let radians = Double(atan2f(Float(view.transform.b), Float(view.transform.a)))
return ceil(radians * 180 / M_PI)
}
extension UIViewController { extension UIViewController {
/// MaterialLayer Reference. /// MaterialLayer Reference.
fileprivate var motionTransition: MotionTransitionItemController { fileprivate var motionTransition: MotionTransitionItemController {
...@@ -128,7 +133,7 @@ extension UIView { ...@@ -128,7 +133,7 @@ extension UIView {
let oldCornerRadius = view.cornerRadius let oldCornerRadius = view.cornerRadius
view.cornerRadius = 0 view.cornerRadius = 0
let oldRotation = view.layer.value(forKeyPath: MotionAnimationKeyPath.rotation.rawValue) as? CGFloat ?? 0 let oldRotation = view.layer.value(forKeyPath: MotionAnimationKeyPath.rotation.rawValue) ?? 0
view.layer.setValue(0, forKeyPath: MotionAnimationKeyPath.rotation.rawValue) view.layer.setValue(0, forKeyPath: MotionAnimationKeyPath.rotation.rawValue)
let v = view.snapshotView(afterScreenUpdates: afterUpdates)! let v = view.snapshotView(afterScreenUpdates: afterUpdates)!
...@@ -158,7 +163,7 @@ extension UIView { ...@@ -158,7 +163,7 @@ extension UIView {
v.shadowColor = view.shadowColor v.shadowColor = view.shadowColor
v.shadowOffset = view.shadowOffset v.shadowOffset = view.shadowOffset
v.contentMode = view.contentMode v.contentMode = view.contentMode
v.layer.transform = view.layer.transform // v.layer.transform = view.layer.transform
view.isHidden = true view.isHidden = true
(view as? Pulseable)?.pulse.pulseLayer?.isHidden = false (view as? Pulseable)?.pulse.pulseLayer?.isHidden = false
...@@ -376,8 +381,8 @@ open class MotionTransitionPresentedAnimator: MotionTransitionAnimator { ...@@ -376,8 +381,8 @@ open class MotionTransitionPresentedAnimator: MotionTransitionAnimator {
snapshotChildAnimations.append(Motion.position(x: w / 2, y: h / 2)) snapshotChildAnimations.append(Motion.position(x: w / 2, y: h / 2))
snapshotChildAnimations.append(sizeAnimation) snapshotChildAnimations.append(sizeAnimation)
let rotateAnimation = Motion.rotate(angle: toView.layer.value(forKeyPath: MotionAnimationKeyPath.rotation.rawValue) as? CGFloat ?? 0) let rotateAnimation = Motion.rotate(angle: getRotationInDegrees(view: toView))
rotateAnimation.fromValue = fromView.layer.value(forKeyPath: MotionAnimationKeyPath.rotation.rawValue) as? CGFloat ?? 0 //rotateAnimation.fromValue = getRotationInDegrees(view: fromView)
snapshotAnimations.append(rotateAnimation) snapshotAnimations.append(rotateAnimation)
let cornerRadiusAnimation = Motion.corner(radius: toView.cornerRadius) let cornerRadiusAnimation = Motion.corner(radius: toView.cornerRadius)
...@@ -492,8 +497,8 @@ open class MotionTransitionDismissedAnimator: MotionTransitionAnimator { ...@@ -492,8 +497,8 @@ open class MotionTransitionDismissedAnimator: MotionTransitionAnimator {
snapshotChildAnimations.append(Motion.position(x: w / 2, y: h / 2)) snapshotChildAnimations.append(Motion.position(x: w / 2, y: h / 2))
snapshotChildAnimations.append(sizeAnimation) snapshotChildAnimations.append(sizeAnimation)
let rotateAnimation = Motion.rotate(angle: toView.layer.value(forKeyPath: MotionAnimationKeyPath.rotation.rawValue) as? CGFloat ?? 0) let rotateAnimation = Motion.rotate(angle: getRotationInDegrees(view: toView))
rotateAnimation.fromValue = fromView.layer.value(forKeyPath: MotionAnimationKeyPath.rotation.rawValue) as? CGFloat ?? 0 //rotateAnimation.fromValue = getRotationInDegrees(view: fromView)
snapshotAnimations.append(rotateAnimation) snapshotAnimations.append(rotateAnimation)
let cornerRadiusAnimation = Motion.corner(radius: toView.cornerRadius) let cornerRadiusAnimation = Motion.corner(radius: toView.cornerRadius)
......
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