Commit 426a8998 by Daniel Dahan

reworked CascadePreprocessor

parent 6df970aa
...@@ -36,6 +36,7 @@ public enum CascadeDirection { ...@@ -36,6 +36,7 @@ public enum CascadeDirection {
case radial(center:CGPoint) case radial(center:CGPoint)
case inverseRadial(center:CGPoint) case inverseRadial(center:CGPoint)
/// Based on the cascade direction a comparator is set.
var comparator: (UIView, UIView) -> Bool { var comparator: (UIView, UIView) -> Bool {
switch self { switch self {
case .topToBottom: case .topToBottom:
...@@ -63,42 +64,52 @@ class CascadePreprocessor: MotionPreprocessor { ...@@ -63,42 +64,52 @@ class CascadePreprocessor: MotionPreprocessor {
/// A reference to a MotionContext. /// A reference to a MotionContext.
weak var context: MotionContext! weak var context: MotionContext!
/**
Processes the from-views and to-views.
- Parameter fromViews: An Array of UIViews.
- Parameter toViews: An Array of UIViews.
*/
func process(fromViews: [UIView], toViews: [UIView]) { func process(fromViews: [UIView], toViews: [UIView]) {
process(views:fromViews) process(views: fromViews)
process(views:toViews) process(views: toViews)
} }
func process(views: [UIView]) { /**
for view in views { Process an Array of views for the cascade animation.
guard let (deltaTime, direction, delayMatchedViews) = context[view]?.cascade else { continue } - Parameter views: An Array of UIViews.
*/
func process(views: [UIView]) {
for v in views {
guard let (deltaTime, direction, delayMatchedViews) = context[v]?.cascade else {
continue
}
var parentView = view var parentView = v is UITableView ? v.subviews.get(0) ?? v : v
if view is UITableView, let wrapperView = view.subviews.get(0) {
parentView = wrapperView
}
let sortedSubviews = parentView.subviews.sorted(by: direction.comparator) let sortedSubviews = parentView.subviews.sorted(by: direction.comparator)
let initialDelay = context[view]!.delay let initialDelay = context[v]!.delay
let finalDelay = TimeInterval(sortedSubviews.count) * deltaTime + initialDelay let finalDelay = TimeInterval(sortedSubviews.count) * deltaTime + initialDelay
for (i, subview) in sortedSubviews.enumerated() { for (i, subview) in sortedSubviews.enumerated() {
let delay = TimeInterval(i) * deltaTime + initialDelay let delay = TimeInterval(i) * deltaTime + initialDelay
func applyDelay(view: UIView) { func applyDelay(view: UIView) {
if context.transitionPairedView(for: view) == nil { if context.transitionPairedView(for: view) == nil {
context[view]?.delay = delay context[view]?.delay = delay
} else if delayMatchedViews, let paired = context.transitionPairedView(for: view) {
context[view]?.delay = finalDelay } else if delayMatchedViews, let paired = context.transitionPairedView(for: view) {
context[paired]?.delay = finalDelay context[view]?.delay = finalDelay
} context[paired]?.delay = finalDelay
for subview in view.subviews { }
applyDelay(view: subview)
} for subview in view.subviews {
} applyDelay(view: subview)
}
}
applyDelay(view: subview) applyDelay(view: subview)
} }
}
} }
}
} }
...@@ -173,15 +173,15 @@ public enum MotionTransitionType { ...@@ -173,15 +173,15 @@ public enum MotionTransitionType {
} }
class TransitionPreprocessor: MotionPreprocessor { class TransitionPreprocessor: MotionPreprocessor {
/// A reference to a MotionContext instance. /// A reference to a MotionContext.
weak var context: MotionContext! weak var context: MotionContext!
/// A reference to a Motion instance. /// A reference to a Motion.
weak var motion: Motion? weak var motion: Motion?
/** /**
An initializer that accepts a given Motion instance. An initializer that accepts a given Motion instance.
- Parameter motion: A Motion instance. - Parameter motion: A Motion.
*/ */
init(motion: Motion) { init(motion: Motion) {
self.motion = motion self.motion = motion
......
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