Commit baf1bf57 by Daniel Dahan

reworked Motion+UIViewController

parent 53088a53
......@@ -53,7 +53,7 @@
/* Begin PBXFileReference section */
963150D11EE50DA6002B0D42 /* Motion+Obj-C.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Motion+Obj-C.swift"; sourceTree = "<group>"; };
963150D41EE51C7A002B0D42 /* MotionAnimation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MotionAnimation.swift; sourceTree = "<group>"; };
963150D91EE51EB4002B0D42 /* MotionAnimationFillMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MotionAnimationFillMode.swift; sourceTree = "<group>"; };
963150D91EE51EB4002B0D42 /* MotionAnimationFillMode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MotionAnimationFillMode.swift; path = ../Extensions/MotionAnimationFillMode.swift; sourceTree = "<group>"; };
968989B81EE5B34B003B8F3D /* MotionHasInsertOrder.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MotionHasInsertOrder.swift; sourceTree = "<group>"; };
968989DB1EE65F2B003B8F3D /* MotionPreprocessor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MotionPreprocessor.swift; sourceTree = "<group>"; };
968989DD1EE6633E003B8F3D /* MotionAnimator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MotionAnimator.swift; sourceTree = "<group>"; };
......@@ -117,6 +117,7 @@
96AEB6671EE4610F009A3BE0 /* MotionDefaultAnimator.swift */,
96AEB6681EE4610F009A3BE0 /* MotionViewPropertyViewContext.swift */,
968989B81EE5B34B003B8F3D /* MotionHasInsertOrder.swift */,
963150D91EE51EB4002B0D42 /* MotionAnimationFillMode.swift */,
);
path = Animator;
sourceTree = "<group>";
......@@ -141,7 +142,6 @@
96AEB6731EE4610F009A3BE0 /* Motion+UIKit.swift */,
96AEB6741EE4610F009A3BE0 /* Motion+UIView.swift */,
96AEB6751EE4610F009A3BE0 /* Motion+UIViewController.swift */,
963150D91EE51EB4002B0D42 /* MotionAnimationFillMode.swift */,
);
path = Extensions;
sourceTree = "<group>";
......
......@@ -49,9 +49,9 @@ internal extension UIView {
}
}
fileprivate var MotionInstanceKey: UInt8 = 0
fileprivate var AssociatedInstanceKey: UInt8 = 0
fileprivate struct MotionInstance {
fileprivate struct AssociatedInstance {
/// A boolean indicating whether Motion is enabled.
fileprivate var isEnabled: Bool
......@@ -69,15 +69,15 @@ fileprivate struct MotionInstance {
}
extension UIView {
/// MotionInstance reference.
fileprivate var motionInstance: MotionInstance {
/// AssociatedInstance reference.
fileprivate var associatedInstance: AssociatedInstance {
get {
return AssociatedObject.get(base: self, key: &MotionInstanceKey) {
return MotionInstance(isEnabled: true, identifier: nil, animations: nil, transitions: nil, alpha: 1)
return AssociatedObject.get(base: self, key: &AssociatedInstanceKey) {
return AssociatedInstance(isEnabled: true, identifier: nil, animations: nil, transitions: nil, alpha: 1)
}
}
set(value) {
AssociatedObject.set(base: self, key: &MotionInstanceKey, value: value)
AssociatedObject.set(base: self, key: &AssociatedInstanceKey, value: value)
}
}
......@@ -85,10 +85,10 @@ extension UIView {
@IBInspectable
public var isMotionEnabled: Bool {
get {
return motionInstance.isEnabled
return associatedInstance.isEnabled
}
set(value) {
motionInstance.isEnabled = value
associatedInstance.isEnabled = value
}
}
......@@ -96,30 +96,30 @@ extension UIView {
@IBInspectable
open var motionIdentifier: String? {
get {
return motionInstance.identifier
return associatedInstance.identifier
}
set(value) {
motionInstance.identifier = value
associatedInstance.identifier = value
}
}
/// The animations to run.
open var motionAnimations: [MotionAnimation]? {
get {
return motionInstance.animations
return associatedInstance.animations
}
set(value) {
motionInstance.animations = value
associatedInstance.animations = value
}
}
/// The animations to run while in transition.
open var motionTransitions: [MotionTransition]? {
get {
return motionInstance.transitions
return associatedInstance.transitions
}
set(value) {
motionInstance.transitions = value
associatedInstance.transitions = value
}
}
......@@ -127,10 +127,10 @@ extension UIView {
@IBInspectable
open var motionAlpha: CGFloat? {
get {
return motionInstance.alpha
return associatedInstance.alpha
}
set(value) {
motionInstance.alpha = value
associatedInstance.alpha = value
}
}
}
......
......@@ -62,7 +62,7 @@ public class Motion: MotionController {
/// progress of the current transition. 0 if no transition is happening
public override var progress: Double {
didSet {
if transitioning {
if isTransitioning {
transitionContext?.updateInteractiveTransition(CGFloat(progress))
}
}
......@@ -70,7 +70,7 @@ public class Motion: MotionController {
public var isAnimating: Bool = false
/// a UIViewControllerContextTransitioning object provided by UIKit,
/// might be nil when transitioning. This happens when calling motionReplaceViewController
/// might be nil when isTransitioning. This happens when calling motionReplaceViewController
internal weak var transitionContext: UIViewControllerContextTransitioning?
internal var fullScreenSnapshot: UIView!
......@@ -129,7 +129,7 @@ public extension Motion {
// internal methods for transition
internal extension Motion {
func start() {
guard transitioning else { return }
guard isTransitioning else { return }
if let fvc = fromViewController, let tvc = toViewController {
closureProcessForMotionDelegate(vc: fvc) {
$0.motionWillStartTransition?()
......@@ -214,7 +214,7 @@ internal extension Motion {
}
override func complete(finished: Bool) {
guard transitioning else { return }
guard isTransitioning else { return }
context.clean()
if finished && presenting && toOverFullScreen {
......@@ -304,7 +304,7 @@ internal extension Motion {
// custom transition helper, used in motion_replaceViewController
internal extension Motion {
func transition(from: UIViewController, to: UIViewController, in view: UIView, completion: ((Bool) -> Void)? = nil) {
guard !transitioning else { return }
guard !isTransitioning else { return }
presenting = true
transitionContainer = view
fromViewController = from
......@@ -341,7 +341,7 @@ internal extension Motion {
extension Motion: UIViewControllerAnimatedTransitioning {
public func animateTransition(using context: UIViewControllerContextTransitioning) {
guard !transitioning else { return }
guard !isTransitioning else { return }
transitionContext = context
fromViewController = fromViewController ?? context.viewController(forKey: .from)
toViewController = toViewController ?? context.viewController(forKey: .to)
......
......@@ -40,7 +40,7 @@ public class MotionController: NSObject {
/// progress of the current transition. 0 if no transition is happening
public internal(set) var progress: Double = 0 {
didSet {
if transitioning {
if isTransitioning {
if let progressUpdateObservers = progressUpdateObservers {
for observer in progressUpdateObservers {
observer.motionDidUpdateProgress(progress: progress)
......@@ -61,12 +61,12 @@ public class MotionController: NSObject {
}
}
/// whether or not we are doing a transition
public var transitioning: Bool {
public var isTransitioning: Bool {
return transitionContainer != nil
}
/// container we created to hold all animating views, will be a subview of the
/// transitionContainer when transitioning
/// transitionContainer when isTransitioning
public internal(set) var container: UIView!
/// this is the container supplied by UIKit
......@@ -98,7 +98,7 @@ public class MotionController: NSObject {
}
}
func displayUpdate(_ link: CADisplayLink) {
if transitioning, duration > 0, let beginTime = beginTime {
if isTransitioning, duration > 0, let beginTime = beginTime {
let elapsedTime = CACurrentMediaTime() - beginTime
if elapsedTime > duration {
......@@ -138,7 +138,7 @@ public extension MotionController {
- progress: the current progress, must be between -1...1
*/
public func update(progress: Double) {
guard transitioning else { return }
guard isTransitioning else { return }
self.beginTime = nil
self.progress = max(-1, min(1, progress))
}
......@@ -149,7 +149,7 @@ public extension MotionController {
current state to the **end** state
*/
public func end(animate: Bool = true) {
guard transitioning else { return }
guard isTransitioning else { return }
if !animate {
self.complete(finished:true)
return
......@@ -167,7 +167,7 @@ public extension MotionController {
current state to the **begining** state
*/
public func cancel(animate: Bool = true) {
guard transitioning else { return }
guard isTransitioning else { return }
if !animate {
self.complete(finished:false)
return
......@@ -196,7 +196,7 @@ public extension MotionController {
- view: the view to override to
*/
public func apply(modifiers: [MotionTransition], to view: UIView) {
guard transitioning else { return }
guard isTransitioning else { return }
let targetState = MotionTargetState(modifiers: modifiers)
if let otherView = self.context.pairedView(for: view) {
for animator in self.animators {
......@@ -233,7 +233,7 @@ internal extension MotionController {
/// must have transitionContainer set already
/// subclass should call context.set(fromViews:toViews) after inserting fromViews & toViews into the container
func prepareForTransition() {
guard transitioning else { fatalError() }
guard isTransitioning else { fatalError() }
plugins = Motion.enabledPlugins.map({ return $0.init() })
processors = [
IgnoreSubviewModifiersPreprocessor(),
......@@ -273,14 +273,14 @@ internal extension MotionController {
}
func processContext() {
guard transitioning else { fatalError() }
guard isTransitioning else { fatalError() }
for processor in processors {
processor.process(fromViews: context.fromViews, toViews: context.toViews)
}
}
func prepareForAnimation() {
guard transitioning else { fatalError() }
guard isTransitioning else { fatalError() }
animatingViews = [([UIView], [UIView])]()
for animator in animators {
let currentFromViews = context.fromViews.filter { (view: UIView) -> Bool in
......@@ -296,7 +296,7 @@ internal extension MotionController {
/// Actually animate the views
/// subclass should call `prepareForTransition` & `prepareForAnimation` before calling `animate`
func animate() {
guard transitioning else { fatalError() }
guard isTransitioning else { fatalError() }
for (currentFromViews, currentToViews) in animatingViews {
// auto hide all animated views
for view in currentFromViews {
......@@ -328,7 +328,7 @@ internal extension MotionController {
}
func complete(after: TimeInterval, finishing: Bool) {
guard transitioning else { fatalError() }
guard isTransitioning else { fatalError() }
if after <= 0.001 {
complete(finished: finishing)
return
......@@ -340,7 +340,7 @@ internal extension MotionController {
}
func complete(finished: Bool) {
guard transitioning else { fatalError() }
guard isTransitioning else { fatalError() }
for animator in animators {
animator.clean()
}
......
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