Commit 7f734fe0 by Daniel Dahan

updated Motion+UIVIewController transition logic

parent 7a639921
...@@ -133,25 +133,26 @@ extension UIViewController { ...@@ -133,25 +133,26 @@ extension UIViewController {
} }
if value { if value {
transitioningDelegate = Motion.shared transitioningDelegate = MotionTransition.shared
if let v = self as? UINavigationController { if let v = self as? UINavigationController {
previousNavigationDelegate = v.delegate previousNavigationDelegate = v.delegate
v.delegate = Motion.shared v.delegate = MotionTransition.shared
} }
if let v = self as? UITabBarController { if let v = self as? UITabBarController {
previousTabBarDelegate = v.delegate previousTabBarDelegate = v.delegate
v.delegate = Motion.shared v.delegate = MotionTransition.shared
} }
} else { } else {
transitioningDelegate = nil transitioningDelegate = nil
if let v = self as? UINavigationController, v.delegate is Motion { if let v = self as? UINavigationController, v.delegate is MotionTransition {
v.delegate = previousNavigationDelegate v.delegate = previousNavigationDelegate
} }
if let v = self as? UITabBarController, v.delegate is Motion { if let v = self as? UITabBarController, v.delegate is MotionTransition {
v.delegate = previousTabBarDelegate v.delegate = previousTabBarDelegate
} }
} }
...@@ -270,7 +271,7 @@ extension UIViewController { ...@@ -270,7 +271,7 @@ extension UIViewController {
// UIKit's UIViewController.dismiss will jump to target.presentedViewController then perform the dismiss. // UIKit's UIViewController.dismiss will jump to target.presentedViewController then perform the dismiss.
// We overcome this behavior by inserting a snapshot into target.presentedViewController // We overcome this behavior by inserting a snapshot into target.presentedViewController
// And also force Hero to use the current VC as the fromViewController // And also force Hero to use the current VC as the fromViewController
Motion.shared.fromViewController = fvc MotionTransition.shared.fromViewController = fvc
let snapshotView = fvc.view.snapshotView(afterScreenUpdates: true)! let snapshotView = fvc.view.snapshotView(afterScreenUpdates: true)!
let targetSuperview = tvc.presentedViewController!.view! let targetSuperview = tvc.presentedViewController!.view!
...@@ -291,7 +292,9 @@ extension UIViewController { ...@@ -291,7 +292,9 @@ extension UIViewController {
- Parameter with next: A UIViewController. - Parameter with next: A UIViewController.
*/ */
public func motionReplaceViewController(with next: UIViewController) { public func motionReplaceViewController(with next: UIViewController) {
guard !Motion.shared.isTransitioning else { let motion = next.transitioningDelegate as? MotionTransition ?? MotionTransition.shared
guard !motion.isTransitioning else {
print("motionReplaceViewController cancelled because Motion was doing a transition. Use Motion.shared.cancel(animated: false) or Motion.shared.end(animated: false) to stop the transition first before calling motionReplaceViewController.") print("motionReplaceViewController cancelled because Motion was doing a transition. Use Motion.shared.cancel(animated: false) or Motion.shared.end(animated: false) to stop the transition first before calling motionReplaceViewController.")
return return
} }
...@@ -305,19 +308,19 @@ extension UIViewController { ...@@ -305,19 +308,19 @@ extension UIViewController {
} }
if nc.isMotionEnabled { if nc.isMotionEnabled {
Motion.shared.forceNonInteractive = true motion.forceNonInteractive = true
} }
nc.setViewControllers(v, animated: true) nc.setViewControllers(v, animated: true)
} else if let container = view.superview { } else if let container = view.superview {
let presentingVC = presentingViewController let presentingVC = presentingViewController
Motion.shared.transition(from: self, to: next, in: container) { [weak self] (isFinishing) in motion.transition(from: self, to: next, in: container) { [weak self] (isFinishing) in
guard isFinishing else { guard isFinishing else {
return return
} }
UIApplication.shared.keyWindow?.addSubview(next.view) next.view.window?.addSubview(next.view)
guard let pvc = presentingVC else { guard let pvc = presentingVC else {
UIApplication.shared.keyWindow?.rootViewController = next UIApplication.shared.keyWindow?.rootViewController = next
......
...@@ -125,15 +125,10 @@ public protocol MotionViewControllerDelegate { ...@@ -125,15 +125,10 @@ public protocol MotionViewControllerDelegate {
func apply(transitions: [MotionTargetState], to view: UIView) func apply(transitions: [MotionTargetState], to view: UIView)
``` ```
*/ */
import UIKit
public class Motion: NSObject {
/// Shared singleton object for controlling the transition
public static let shared = MotionTransition()
}
public typealias MotionCancelBlock = (Bool) -> Void public typealias MotionCancelBlock = (Bool) -> Void
public class Motion: NSObject {}
extension Motion { extension Motion {
/** /**
Executes a block of code asynchronously on the main thread. Executes a block of code asynchronously on the main thread.
...@@ -241,6 +236,9 @@ public enum MotionTransitionState: Int { ...@@ -241,6 +236,9 @@ public enum MotionTransitionState: Int {
} }
open class MotionTransition: NSObject { open class MotionTransition: NSObject {
/// Shared singleton object for controlling the transition
public static let shared = MotionTransition()
/// Default animation type. /// Default animation type.
internal var defaultAnimation = MotionTransitionAnimationType.auto internal var defaultAnimation = MotionTransitionAnimationType.auto
......
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