Commit ae0a2079 by Daniel Dahan

moved Motion extension to MotionBasicAnimation

parent 6050b9bc
...@@ -571,38 +571,38 @@ extension MotionAnimator { ...@@ -571,38 +571,38 @@ extension MotionAnimator {
var snapshotAnimations = [CABasicAnimation]() var snapshotAnimations = [CABasicAnimation]()
var snapshotChildAnimations = [CABasicAnimation]() var snapshotChildAnimations = [CABasicAnimation]()
let sizeAnimation = Motion.size(to.bounds.size) let sizeAnimation = MotionBasicAnimation.size(to.bounds.size)
let cornerRadiusAnimation = Motion.corner(radius: to.layer.cornerRadius) let cornerRadiusAnimation = MotionBasicAnimation.corner(radius: to.layer.cornerRadius)
snapshotAnimations.append(sizeAnimation) snapshotAnimations.append(sizeAnimation)
snapshotAnimations.append(cornerRadiusAnimation) snapshotAnimations.append(cornerRadiusAnimation)
snapshotAnimations.append(Motion.position(to: to.motionPosition)) snapshotAnimations.append(MotionBasicAnimation.position(to: to.motionPosition))
snapshotAnimations.append(Motion.transform(transform: to.motionTransform)) snapshotAnimations.append(MotionBasicAnimation.transform(transform: to.motionTransform))
snapshotAnimations.append(Motion.background(color: to.backgroundColor ?? .clear)) snapshotAnimations.append(MotionBasicAnimation.background(color: to.backgroundColor ?? .clear))
snapshotAnimations.append(Motion.border(color: to.borderColor ?? .clear)) snapshotAnimations.append(MotionBasicAnimation.border(color: to.borderColor ?? .clear))
snapshotAnimations.append(Motion.border(width: to.borderWidth)) snapshotAnimations.append(MotionBasicAnimation.border(width: to.borderWidth))
if let path = to.layer.shadowPath { if let path = to.layer.shadowPath {
let shadowPath = Motion.shadow(path: path) let shadowPath = MotionBasicAnimation.shadow(path: path)
shadowPath.fromValue = fromView.layer.shadowPath shadowPath.fromValue = fromView.layer.shadowPath
snapshotAnimations.append(shadowPath) snapshotAnimations.append(shadowPath)
} }
let shadowOffset = Motion.shadow(offset: to.layer.shadowOffset) let shadowOffset = MotionBasicAnimation.shadow(offset: to.layer.shadowOffset)
shadowOffset.fromValue = fromView.layer.shadowOffset shadowOffset.fromValue = fromView.layer.shadowOffset
snapshotAnimations.append(shadowOffset) snapshotAnimations.append(shadowOffset)
let shadowOpacity = Motion.shadow(opacity: to.layer.shadowOpacity) let shadowOpacity = MotionBasicAnimation.shadow(opacity: to.layer.shadowOpacity)
shadowOpacity.fromValue = fromView.layer.shadowOpacity shadowOpacity.fromValue = fromView.layer.shadowOpacity
snapshotAnimations.append(shadowOpacity) snapshotAnimations.append(shadowOpacity)
let shadowRadius = Motion.shadow(radius: to.layer.shadowRadius) let shadowRadius = MotionBasicAnimation.shadow(radius: to.layer.shadowRadius)
shadowRadius.fromValue = fromView.layer.shadowRadius shadowRadius.fromValue = fromView.layer.shadowRadius
snapshotAnimations.append(shadowRadius) snapshotAnimations.append(shadowRadius)
snapshotChildAnimations.append(cornerRadiusAnimation) snapshotChildAnimations.append(cornerRadiusAnimation)
snapshotChildAnimations.append(sizeAnimation) snapshotChildAnimations.append(sizeAnimation)
snapshotChildAnimations.append(Motion.position(x: to.bounds.width / 2, y: to.bounds.height / 2)) snapshotChildAnimations.append(MotionBasicAnimation.position(x: to.bounds.width / 2, y: to.bounds.height / 2))
let d = calculateAnimationTransitionDuration(animations: to.motionAnimations) let d = calculateAnimationTransitionDuration(animations: to.motionAnimations)
......
...@@ -265,97 +265,97 @@ extension CALayer { ...@@ -265,97 +265,97 @@ extension CALayer {
case let .custom(animation): case let .custom(animation):
a.append(animation) a.append(animation)
case let .backgroundColor(color): case let .backgroundColor(color):
a.append(Motion.background(color: color)) a.append(MotionBasicAnimation.background(color: color))
case let .barTintColor(color): case let .barTintColor(color):
a.append(Motion.barTint(color: color)) a.append(MotionBasicAnimation.barTint(color: color))
case let .borderColor(color): case let .borderColor(color):
a.append(Motion.border(color: color)) a.append(MotionBasicAnimation.border(color: color))
case let .borderWidth(width): case let .borderWidth(width):
a.append(Motion.border(width: width)) a.append(MotionBasicAnimation.border(width: width))
case let .cornerRadius(radius): case let .cornerRadius(radius):
a.append(Motion.corner(radius: radius)) a.append(MotionBasicAnimation.corner(radius: radius))
case let .transform(transform): case let .transform(transform):
a.append(Motion.transform(transform: transform)) a.append(MotionBasicAnimation.transform(transform: transform))
case let .rotationAngle(angle): case let .rotationAngle(angle):
let rotate = Motion.rotation(angle: angle) let rotate = MotionBasicAnimation.rotation(angle: angle)
a.append(rotate) a.append(rotate)
case let .rotationAngleX(angle): case let .rotationAngleX(angle):
a.append(Motion.rotationX(angle: angle)) a.append(MotionBasicAnimation.rotationX(angle: angle))
case let .rotationAngleY(angle): case let .rotationAngleY(angle):
a.append(Motion.rotationY(angle: angle)) a.append(MotionBasicAnimation.rotationY(angle: angle))
case let .rotationAngleZ(angle): case let .rotationAngleZ(angle):
a.append(Motion.rotationZ(angle: angle)) a.append(MotionBasicAnimation.rotationZ(angle: angle))
case let .spin(rotations): case let .spin(rotations):
a.append(Motion.spin(rotations: rotations)) a.append(MotionBasicAnimation.spin(rotations: rotations))
case let .spinX(rotations): case let .spinX(rotations):
a.append(Motion.spinX(rotations: rotations)) a.append(MotionBasicAnimation.spinX(rotations: rotations))
case let .spinY(rotations): case let .spinY(rotations):
a.append(Motion.spinY(rotations: rotations)) a.append(MotionBasicAnimation.spinY(rotations: rotations))
case let .spinZ(rotations): case let .spinZ(rotations):
a.append(Motion.spinZ(rotations: rotations)) a.append(MotionBasicAnimation.spinZ(rotations: rotations))
case let .scale(to): case let .scale(to):
a.append(Motion.scale(to: to)) a.append(MotionBasicAnimation.scale(to: to))
case let .scaleX(to): case let .scaleX(to):
a.append(Motion.scaleX(to: to)) a.append(MotionBasicAnimation.scaleX(to: to))
case let .scaleY(to): case let .scaleY(to):
a.append(Motion.scaleY(to: to)) a.append(MotionBasicAnimation.scaleY(to: to))
case let .scaleZ(to): case let .scaleZ(to):
a.append(Motion.scaleZ(to: to)) a.append(MotionBasicAnimation.scaleZ(to: to))
case let .translate(x, y): case let .translate(x, y):
a.append(Motion.translate(to: CGPoint(x: x, y: y))) a.append(MotionBasicAnimation.translate(to: CGPoint(x: x, y: y)))
case let .translateX(to): case let .translateX(to):
a.append(Motion.translateX(to: to)) a.append(MotionBasicAnimation.translateX(to: to))
case let .translateY(to): case let .translateY(to):
a.append(Motion.translateY(to: to)) a.append(MotionBasicAnimation.translateY(to: to))
case let .translateZ(to): case let .translateZ(to):
a.append(Motion.translateZ(to: to)) a.append(MotionBasicAnimation.translateZ(to: to))
case .x(_), .y(_), .point(_, _): case .x(_), .y(_), .point(_, _):
let position = Motion.position(to: CGPoint(x: px, y: py)) let position = MotionBasicAnimation.position(to: CGPoint(x: px, y: py))
a.append(position) a.append(position)
case let .position(x, y): case let .position(x, y):
a.append(Motion.position(to: CGPoint(x: x, y: y))) a.append(MotionBasicAnimation.position(to: CGPoint(x: x, y: y)))
case let .fade(opacity): case let .fade(opacity):
let fade = Motion.fade(opacity: opacity) let fade = MotionBasicAnimation.fade(opacity: opacity)
fade.fromValue = s.value(forKey: MotionAnimationKeyPath.opacity.rawValue) ?? NSNumber(floatLiteral: 1) fade.fromValue = s.value(forKey: MotionAnimationKeyPath.opacity.rawValue) ?? NSNumber(floatLiteral: 1)
a.append(fade) a.append(fade)
case let .zPosition(index): case let .zPosition(index):
let zPosition = Motion.zPosition(index: index) let zPosition = MotionBasicAnimation.zPosition(index: index)
zPosition.fromValue = s.value(forKey: MotionAnimationKeyPath.zPosition.rawValue) ?? NSNumber(integerLiteral: 0) zPosition.fromValue = s.value(forKey: MotionAnimationKeyPath.zPosition.rawValue) ?? NSNumber(integerLiteral: 0)
a.append(zPosition) a.append(zPosition)
case .width(_), .height(_), .size(_, _): case .width(_), .height(_), .size(_, _):
a.append(Motion.size(CGSize(width: w, height: h))) a.append(MotionBasicAnimation.size(CGSize(width: w, height: h)))
case let .shadowPath(path): case let .shadowPath(path):
let shadowPath = Motion.shadow(path: path) let shadowPath = MotionBasicAnimation.shadow(path: path)
shadowPath.fromValue = s.shadowPath shadowPath.fromValue = s.shadowPath
a.append(shadowPath) a.append(shadowPath)
case let .shadowOffset(offset): case let .shadowOffset(offset):
let shadowOffset = Motion.shadow(offset: offset) let shadowOffset = MotionBasicAnimation.shadow(offset: offset)
shadowOffset.fromValue = s.shadowOffset shadowOffset.fromValue = s.shadowOffset
a.append(shadowOffset) a.append(shadowOffset)
case let .shadowOpacity(opacity): case let .shadowOpacity(opacity):
let shadowOpacity = Motion.shadow(opacity: opacity) let shadowOpacity = MotionBasicAnimation.shadow(opacity: opacity)
shadowOpacity.fromValue = s.shadowOpacity shadowOpacity.fromValue = s.shadowOpacity
a.append(shadowOpacity) a.append(shadowOpacity)
case let .shadowRadius(radius): case let .shadowRadius(radius):
let shadowRadius = Motion.shadow(radius: radius) let shadowRadius = MotionBasicAnimation.shadow(radius: radius)
shadowRadius.fromValue = s.shadowRadius shadowRadius.fromValue = s.shadowRadius
a.append(shadowRadius) a.append(shadowRadius)
case let .depth(offset, opacity, radius): case let .depth(offset, opacity, radius):
if let path = s.shadowPath { if let path = s.shadowPath {
let shadowPath = Motion.shadow(path: path) let shadowPath = MotionBasicAnimation.shadow(path: path)
shadowPath.fromValue = s.shadowPath shadowPath.fromValue = s.shadowPath
a.append(shadowPath) a.append(shadowPath)
} }
let shadowOffset = Motion.shadow(offset: offset) let shadowOffset = MotionBasicAnimation.shadow(offset: offset)
shadowOffset.fromValue = s.shadowOffset shadowOffset.fromValue = s.shadowOffset
a.append(shadowOffset) a.append(shadowOffset)
let shadowOpacity = Motion.shadow(opacity: opacity) let shadowOpacity = MotionBasicAnimation.shadow(opacity: opacity)
shadowOpacity.fromValue = s.shadowOpacity shadowOpacity.fromValue = s.shadowOpacity
a.append(shadowOpacity) a.append(shadowOpacity)
let shadowRadius = Motion.shadow(radius: radius) let shadowRadius = MotionBasicAnimation.shadow(radius: radius)
shadowRadius.fromValue = s.shadowRadius shadowRadius.fromValue = s.shadowRadius
a.append(shadowRadius) a.append(shadowRadius)
default:break default:break
...@@ -462,7 +462,30 @@ extension CABasicAnimation { ...@@ -462,7 +462,30 @@ extension CABasicAnimation {
} }
} }
extension Motion { extension CAKeyframeAnimation {
/**
A convenience initializer that takes a given MotionAnimationKeyPath.
- Parameter keyPath: An MotionAnimationKeyPath.
*/
public convenience init(keyPath: MotionAnimationKeyPath) {
self.init(keyPath: keyPath.rawValue)
}
}
public struct MotionKeyframeAnimation {
/**
Creates a CABasicAnimation for the backgroundColor key path.
- Parameter color: A UIColor.
- Returns: A CABasicAnimation.
*/
public static func background(color: UIColor) -> CAKeyframeAnimation {
let animation = CAKeyframeAnimation(keyPath: .backgroundColor)
animation.values = [color.cgColor]
return animation
}
}
public struct MotionBasicAnimation {
/** /**
Creates a CABasicAnimation for the backgroundColor key path. Creates a CABasicAnimation for the backgroundColor key path.
- Parameter color: A UIColor. - Parameter color: A UIColor.
......
...@@ -110,6 +110,7 @@ open class PageMenuController: UIViewController { ...@@ -110,6 +110,7 @@ open class PageMenuController: UIViewController {
} }
prepareViewControllers() prepareViewControllers()
prepareTabBar()
layoutSubviews() layoutSubviews()
} }
} }
...@@ -133,7 +134,11 @@ open class PageMenuController: UIViewController { ...@@ -133,7 +134,11 @@ open class PageMenuController: UIViewController {
fileprivate var previousContentOffset: CGFloat = 0 fileprivate var previousContentOffset: CGFloat = 0
/// The number of views used in the scrollViewPool. /// The number of views used in the scrollViewPool.
fileprivate let viewPoolCount = 3 open var viewPoolCount = 3 {
didSet {
layoutSubviews()
}
}
/** /**
An initializer that accepts an Array of UIViewControllers. An initializer that accepts an Array of UIViewControllers.
...@@ -177,6 +182,7 @@ open class PageMenuController: UIViewController { ...@@ -177,6 +182,7 @@ open class PageMenuController: UIViewController {
view.contentScaleFactor = Screen.scale view.contentScaleFactor = Screen.scale
prepareScrollView() prepareScrollView()
prepareViewControllers() prepareViewControllers()
prepareTabBar()
} }
} }
...@@ -218,8 +224,6 @@ extension PageMenuController { ...@@ -218,8 +224,6 @@ extension PageMenuController {
prepareViewController(at: selectedIndex - 1) prepareViewController(at: selectedIndex - 1)
prepareViewController(at: selectedIndex + 1) prepareViewController(at: selectedIndex + 1)
} }
prepareTabBar()
} }
/** /**
...@@ -284,19 +288,32 @@ extension PageMenuController { ...@@ -284,19 +288,32 @@ extension PageMenuController {
vc.view.contentScaleFactor = Screen.scale vc.view.contentScaleFactor = Screen.scale
scrollView.addSubview(vc.view) scrollView.addSubview(vc.view)
} }
/**
Transitions from one view controller to another.
- Parameter from: The index of the view controller to transition from.
- Parameter to: The index of the view controller to transition to.
*/
fileprivate func prepareViewControllersForTransition(from: Int, to: Int) {
let fromVC = viewControllers[from]
let toVC = viewControllers[to]
fromVC.present(toVC, animated: true)
}
} }
extension PageMenuController { extension PageMenuController {
fileprivate func layoutScrollView() { fileprivate func layoutScrollView() {
scrollView.contentSize = CGSize(width: scrollView.width * CGFloat(viewControllers.count), height: scrollView.height) let w = view.bounds.width
scrollView.contentOffset = CGPoint(x: scrollView.width * CGFloat(selectedIndex), y: 0) scrollView.contentSize = CGSize(width: w * CGFloat(viewControllers.count), height: scrollView.height)
scrollView.contentOffset = CGPoint(x: w * CGFloat(selectedIndex), y: 0)
guard let v = tabBar else { guard let v = tabBar else {
scrollView.frame = view.bounds scrollView.frame = view.bounds
return return
} }
let p = tabBar?.height ?? 0 let p = v.height
let y = view.height - p let y = view.height - p
switch tabBarAlignment { switch tabBarAlignment {
...@@ -310,6 +327,8 @@ extension PageMenuController { ...@@ -310,6 +327,8 @@ extension PageMenuController {
scrollView.y = 0 scrollView.y = 0
scrollView.height = view.height scrollView.height = view.height
} }
scrollView.width = w
} }
fileprivate func layoutViewControllers() { fileprivate func layoutViewControllers() {
...@@ -344,7 +363,8 @@ extension PageMenuController { ...@@ -344,7 +363,8 @@ extension PageMenuController {
- Parameter position: An Int for the position of the view controller. - Parameter position: An Int for the position of the view controller.
*/ */
fileprivate func layoutViewController(at index: Int, position: Int) { fileprivate func layoutViewController(at index: Int, position: Int) {
viewControllers[index].view.frame = CGRect(x: CGFloat(position) * scrollView.width, y: 0, width: scrollView.width, height: scrollView.height) let w = scrollView.width
viewControllers[index].view.frame = CGRect(x: CGFloat(position) * w, y: 0, width: w, height: scrollView.height)
} }
/** /**
...@@ -355,20 +375,20 @@ extension PageMenuController { ...@@ -355,20 +375,20 @@ extension PageMenuController {
return return
} }
let p = tabBar?.height ?? 0 let p = v.height
let y = view.height - p let y = view.height - p
tabBar?.width = view.width v.width = view.width
switch tabBarAlignment { switch tabBarAlignment {
case .top: case .top:
tabBar?.isHidden = false v.isHidden = false
tabBar?.y = 0 v.y = 0
case .bottom: case .bottom:
tabBar?.isHidden = false v.isHidden = false
tabBar?.y = y v.y = y
case .hidden: case .hidden:
tabBar?.isHidden = true v.isHidden = true
} }
} }
} }
...@@ -411,13 +431,13 @@ extension PageMenuController { ...@@ -411,13 +431,13 @@ extension PageMenuController {
- Parameter at index: An Int for the view controller position. - Parameter at index: An Int for the view controller position.
*/ */
fileprivate func removeViewController(at index: Int) { fileprivate func removeViewController(at index: Int) {
let vc = viewControllers[index] let v = viewControllers[index]
guard childViewControllers.contains(vc) else { guard childViewControllers.contains(v) else {
return return
} }
removeViewController(viewController: vc) removeViewController(viewController: v)
} }
/** /**
...@@ -450,12 +470,15 @@ extension PageMenuController { ...@@ -450,12 +470,15 @@ extension PageMenuController {
return return
} }
removeViewControllers()
prepareViewControllersForTransition(from: selectedIndex, to: i)
selectedIndex = i selectedIndex = i
v.select(at: i) v.select(at: i)
removeViewControllers() //prepareTabBar()
prepareViewControllers() //layoutSubviews()
layoutSubviews()
} }
} }
...@@ -469,6 +492,7 @@ extension PageMenuController: UIScrollViewDelegate { ...@@ -469,6 +492,7 @@ extension PageMenuController: UIScrollViewDelegate {
removeViewControllers() removeViewControllers()
prepareViewControllers() prepareViewControllers()
prepareTabBar()
layoutSubviews() layoutSubviews()
} }
} }
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