Commit e6a3b296 by Daniel Dahan

development: added MotionTransition properties to UIViews

parent ca611b57
......@@ -69,6 +69,11 @@ open class CollectionViewController: UIViewController {
prepare()
}
open override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
collectionView.frame = view.bounds
}
/**
Prepares the view instance when intialized. When subclassing,
it is recommended to override the prepareView method
......@@ -89,7 +94,7 @@ extension CollectionViewController {
fileprivate func prepareCollectionView() {
collectionView.delegate = self
collectionView.dataSource = self
view.layout(collectionView).edges()
view.addSubview(collectionView)
}
}
......
......@@ -30,8 +30,48 @@
import UIKit
@objc(AnimationFillMode)
public enum AnimationFillMode: Int {
/// A memory reference to the MotionIdentifier instance for UIView extensions.
fileprivate var MotionIdentifierKey: UInt8 = 0
fileprivate struct MotionTransitionItem {
fileprivate var identifier: String
fileprivate var animations: [MotionAnimation]
}
extension UIView {
/// MaterialLayer Reference.
fileprivate var motionTransitionItem: MotionTransitionItem {
get {
return AssociatedObject(base: self, key: &MotionIdentifierKey) {
return MotionTransitionItem(identifier: "", animations: [])
}
}
set(value) {
AssociateObject(base: self, key: &MotionIdentifierKey, value: value)
}
}
open var motionIdentifier: String {
get {
return motionTransitionItem.identifier
}
set(value) {
motionTransitionItem.identifier = value
}
}
open var motionAnimations: [MotionAnimation] {
get {
return motionTransitionItem.animations
}
set(value) {
motionTransitionItem.animations = value
}
}
}
@objc(MotionAnimationFillMode)
public enum MotionAnimationFillMode: Int {
case forwards
case backwards
case both
......@@ -39,10 +79,10 @@ public enum AnimationFillMode: Int {
}
/**
Converts the AnimationFillMode enum value to a corresponding String.
- Parameter mode: An AnimationFillMode enum value.
Converts the MotionAnimationFillMode enum value to a corresponding String.
- Parameter mode: An MotionAnimationFillMode enum value.
*/
public func AnimationFillModeToValue(mode: AnimationFillMode) -> String {
public func MotionAnimationFillModeToValue(mode: MotionAnimationFillMode) -> String {
switch mode {
case .forwards:
return kCAFillModeForwards
......@@ -162,7 +202,7 @@ public struct Motion {
*/
public static func animate(group animations: [CAAnimation], timingFunction: MotionAnimationTimingFunction = .easeInEaseOut, duration: CFTimeInterval = 0.5) -> CAAnimationGroup {
let group = CAAnimationGroup()
group.fillMode = AnimationFillModeToValue(mode: .forwards)
group.fillMode = MotionAnimationFillModeToValue(mode: .forwards)
group.isRemovedOnCompletion = false
group.animations = animations
group.duration = duration
......
......@@ -220,7 +220,7 @@ extension CALayer {
}
let g = Motion.animate(group: a, duration: duration)
g.fillMode = AnimationFillModeToValue(mode: .forwards)
g.fillMode = MotionAnimationFillModeToValue(mode: .forwards)
g.isRemovedOnCompletion = false
g.timingFunction = MotionAnimationTimingFunctionToValue(timingFunction: timingFunction)
......
......@@ -56,11 +56,11 @@ open class MotionTransitionDelegate: NSObject, UIViewControllerTransitioningDele
open class FadeMotionTransition: NSObject, UIViewControllerAnimatedTransitioning {
open func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {
guard let fromView = transitionContext.view(forKey: .from) else {
return
}
guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {
guard let toView = transitionContext.view(forKey: .to) else {
return
}
......@@ -74,8 +74,7 @@ open class FadeMotionTransition: NSObject, UIViewControllerAnimatedTransitioning
toView.alpha = 1
fromView.alpha = 0
}) { _ in
let success = !transitionContext.transitionWasCancelled
transitionContext.completeTransition(success)
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
......@@ -89,27 +88,49 @@ open class FadeMotionTransition: NSObject, UIViewControllerAnimatedTransitioning
}
open class SlideMotionTransition: NSObject, UIViewControllerAnimatedTransitioning {
var operation: UINavigationControllerOperation
init(operation: UINavigationControllerOperation) {
self.operation = operation
}
open func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else {
guard let fromView = transitionContext.view(forKey: .from) else {
return
}
guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else {
guard let toView = transitionContext.view(forKey: .to) else {
return
}
toView.y = fromView.height
transitionContext.containerView.addSubview(fromView)
transitionContext.containerView.addSubview(toView)
if operation == .push {
transitionContext.containerView.addSubview(fromView)
for v in fromView.subviews {
if 0 < v.motionIdentifier.utf16.count {
v.motion(duration: 0.35, animations: v.motionAnimations)
}
}
Motion.delay(time: transitionDuration(using: nil)) {
transitionContext.containerView.addSubview(toView)
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
UIView.animate(withDuration: transitionDuration(using: transitionContext),
animations: { _ in
toView.frame = fromView.frame
// fromView.alpha = 0
}) { _ in
let success = !transitionContext.transitionWasCancelled
transitionContext.completeTransition(success)
if operation == .pop {
transitionContext.containerView.addSubview(toView)
for v in toView.subviews {
if 0 < v.motionIdentifier.utf16.count {
v.motion(duration: 0.35, animations: [.scale(1), .backgroundColor(.white)])
}
}
Motion.delay(time: transitionDuration(using: nil)) {
transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
}
}
}
......
......@@ -136,7 +136,7 @@ open class NavigationController: UINavigationController {
extension NavigationController: UINavigationControllerDelegate {
open func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return SlideMotionTransition()
return SlideMotionTransition(operation: operation)
}
}
......
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