Commit 61eb033d by Daniel Dahan

updated Motion+UIViewController winding methods

parent 25b99d44
...@@ -189,37 +189,37 @@ extension UIViewController { ...@@ -189,37 +189,37 @@ extension UIViewController {
is contained inside a navigationController is contained inside a navigationController
*/ */
@IBAction @IBAction
public func motion_dismissViewController() { public func motionDismissViewController() {
if let navigationController = navigationController, navigationController.viewControllers.first != self { if let v = navigationController, self != v.viewControllers.first {
navigationController.popViewController(animated: true) v.popViewController(animated: true)
} else { } else {
dismiss(animated: true, completion: nil) dismiss(animated: true)
} }
} }
/// Unwind to the root view controller using Motion. /// Unwind to the root view controller using Motion.
@IBAction @IBAction
public func motion_unwindToRootViewController() { public func motionUnwindToRootViewController() {
motion_unwindToViewController { $0.presentingViewController == nil } motionUnwindToViewController { $0.presentingViewController == nil }
} }
/// Unwind to a specific view controller using Motion. /// Unwind to a specific view controller using Motion.
public func motion_unwindToViewController(_ toViewController: UIViewController) { public func motionUnwindToViewController(_ toViewController: UIViewController) {
motion_unwindToViewController { $0 == toViewController } motionUnwindToViewController { $0 == toViewController }
} }
/// Unwind to a view controller that responds to the given selector using Motion. /// Unwind to a view controller that responds to the given selector using Motion.
public func motion_unwindToViewController(withSelector: Selector) { public func motionUnwindToViewController(withSelector: Selector) {
motion_unwindToViewController { $0.responds(to: withSelector) } motionUnwindToViewController { $0.responds(to: withSelector) }
} }
/// Unwind to a view controller with given class using Motion /// Unwind to a view controller with given class using Motion
public func motion_unwindToViewController(withClass: AnyClass) { public func motionUnwindToViewController(withClass: AnyClass) {
motion_unwindToViewController { $0.isKind(of: withClass) } motionUnwindToViewController { $0.isKind(of: withClass) }
} }
/// Unwind to a view controller that the matchBlock returns true on. /// Unwind to a view controller that the matchBlock returns true on.
public func motion_unwindToViewController(withMatchBlock: (UIViewController) -> Bool) { public func motionUnwindToViewController(withMatchBlock: (UIViewController) -> Bool) {
var target: UIViewController? var target: UIViewController?
var current: UIViewController? = self var current: UIViewController? = self
...@@ -257,7 +257,7 @@ extension UIViewController { ...@@ -257,7 +257,7 @@ extension UIViewController {
v.navigationController?.popToViewController(v, animated: false) v.navigationController?.popToViewController(v, animated: false)
let fromVC = self.navigationController ?? self let fromVC = navigationController ?? self
let toVC = v.navigationController ?? v let toVC = v.navigationController ?? v
if v.presentedViewController != fromVC { if v.presentedViewController != fromVC {
...@@ -275,35 +275,35 @@ extension UIViewController { ...@@ -275,35 +275,35 @@ extension UIViewController {
toVC.presentedViewController?.view.addSubview(snapshot) toVC.presentedViewController?.view.addSubview(snapshot)
} }
toVC.dismiss(animated: true, completion: nil) toVC.dismiss(animated: true)
} }
/** /**
Replace the current view controller with another view controller on the Replace the current view controller with another view controller within the
navigation/modal stack. navigation/modal stack.
- Parameter with next: A UIViewController. - Parameter with next: A UIViewController.
*/ */
public func motion_replaceViewController(with next: UIViewController) { public func motionReplaceViewController(with next: UIViewController) {
guard !Motion.shared.isTransitioning else { guard !Motion.shared.isTransitioning else {
print("motion_replaceViewController 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 motion_replaceViewController.") 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
} }
if let navigationController = navigationController { if let nc = navigationController {
var v = navigationController.childViewControllers var v = nc.childViewControllers
if !v.isEmpty { if !v.isEmpty {
v.removeLast() v.removeLast()
v.append(next) v.append(next)
} }
if navigationController.isMotionEnabled { if nc.isMotionEnabled {
Motion.shared.forceNonInteractive = true Motion.shared.forceNonInteractive = true
} }
navigationController.setViewControllers(v, animated: true) nc.setViewControllers(v, animated: true)
} else if let container = view.superview { } else if let container = view.superview {
let parentVC = presentingViewController let presentingVC = presentingViewController
Motion.shared.transition(from: self, to: next, in: container) { [weak self] (isFinished) in Motion.shared.transition(from: self, to: next, in: container) { [weak self] (isFinished) in
guard isFinished else { guard isFinished else {
...@@ -312,13 +312,13 @@ extension UIViewController { ...@@ -312,13 +312,13 @@ extension UIViewController {
UIApplication.shared.keyWindow?.addSubview(next.view) UIApplication.shared.keyWindow?.addSubview(next.view)
guard let parentVC = parentVC else { guard let pvc = presentingVC else {
UIApplication.shared.keyWindow?.rootViewController = next UIApplication.shared.keyWindow?.rootViewController = next
return return
} }
self?.dismiss(animated: false) { self?.dismiss(animated: false) {
parentVC.present(next, animated: false, completion: nil) pvc.present(next, animated: false)
} }
} }
} }
......
...@@ -154,7 +154,7 @@ public class Motion: MotionController { ...@@ -154,7 +154,7 @@ public class Motion: MotionController {
/** /**
A UIViewControllerContextTransitioning object provided by UIKit, which A UIViewControllerContextTransitioning object provided by UIKit, which
might be nil when isTransitioning. This happens when calling motion_replaceViewController might be nil when isTransitioning. This happens when calling motionReplaceViewController
*/ */
internal weak var transitionContext: UIViewControllerContextTransitioning? internal weak var transitionContext: UIViewControllerContextTransitioning?
...@@ -169,7 +169,7 @@ public class Motion: MotionController { ...@@ -169,7 +169,7 @@ public class Motion: MotionController {
/** /**
By default, Motion will always appear to be interactive to UIKit. This forces it to appear non-interactive. By default, Motion will always appear to be interactive to UIKit. This forces it to appear non-interactive.
Used when doing a motion_replaceViewController within a UINavigationController, to fix a bug with Used when doing a motionReplaceViewController within a UINavigationController, to fix a bug with
UINavigationController.setViewControllers not able to handle interactive transitions. UINavigationController.setViewControllers not able to handle interactive transitions.
*/ */
internal var forceNonInteractive = false internal var forceNonInteractive = false
......
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