Commit e6a3b296 by Daniel Dahan

development: added MotionTransition properties to UIViews

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