Skip to content

Commit

Permalink
Refactor to move view hierarchy logic out of the presentation control…
Browse files Browse the repository at this point in the history
…ler and into the animation controller. Not moving it to the transition controller since it needs to happen before the animation is fully completed.
  • Loading branch information
jonkykong committed Oct 27, 2019
1 parent 4b01b0f commit 8ecc8ab
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
29 changes: 28 additions & 1 deletion Pod/Classes/SideMenuAnimationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal final class SideMenuAnimationController: NSObject, UIViewControllerAnim
private var config: Model
private weak var containerView: UIView?
private let leftSide: Bool
private weak var originalSuperview: UIView?
private var presentationController: SideMenuPresentationController!
private unowned var presentedViewController: UIViewController?
private unowned var presentingViewController: UIViewController?
Expand Down Expand Up @@ -82,6 +83,7 @@ internal final class SideMenuAnimationController: NSObject, UIViewControllerAnim
}

func transition(presenting: Bool, animated: Bool = true, interactive: Bool = false, alongsideTransition: (() -> Void)? = nil, complete: Bool = true, completion: ((Bool) -> Void)? = nil) {
prepare(presenting: presenting)
transitionWillBegin(presenting: presenting)
transition(
presenting: presenting,
Expand All @@ -95,6 +97,7 @@ internal final class SideMenuAnimationController: NSObject, UIViewControllerAnim
guard let self = self else { return }
if complete {
self.transitionDidEnd(presenting: presenting, completed: true)
self.finish(presenting: presenting, completed: true)
}
completion?(true)
})
Expand All @@ -112,10 +115,21 @@ private extension SideMenuAnimationController {
return presenting ? config.presentDuration : config.dismissDuration
}

func prepare(presenting: Bool) {
guard
presenting,
let presentingViewController = presentingViewController,
let presentedViewController = presentedViewController
else { return }

originalSuperview = presentingViewController.view.superview
containerView?.addSubview(presentingViewController.view)
containerView?.addSubview(presentedViewController.view)
}

func transitionWillBegin(presenting: Bool) {
// prevent any other menu gestures from firing
containerView?.isUserInteractionEnabled = false

if presenting {
presentationController.presentationTransitionWillBegin()
} else {
Expand All @@ -140,7 +154,17 @@ private extension SideMenuAnimationController {
containerView?.isUserInteractionEnabled = true
}

func finish(presenting: Bool, completed: Bool) {
guard
presenting != completed,
let presentingViewController = self.presentingViewController
else { return }
presentedViewController?.view.removeFromSuperview()
originalSuperview?.addSubview(presentingViewController.view)
}

func transition(using transitionContext: UIViewControllerContextTransitioning) {
prepare(presenting: transitionContext.isPresenting)
transitionWillBegin(presenting: transitionContext.isPresenting)
transition(
presenting: transitionContext.isPresenting,
Expand All @@ -153,6 +177,9 @@ private extension SideMenuAnimationController {
guard let self = self else { return }
let completed = !transitionContext.transitionWasCancelled
self.transitionDidEnd(presenting: transitionContext.isPresenting, completed: completed)
self.finish(presenting: transitionContext.isPresenting, completed: completed)

// Called last. This causes the transition container to be removed and animationEnded() to be called.
transitionContext.completeTransition(completed)
})
}
Expand Down
7 changes: 0 additions & 7 deletions Pod/Classes/SideMenuPresentationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ internal final class SideMenuPresentationController {
private var interactivePopGestureRecognizerEnabled: Bool?
private var clipsToBounds: Bool?
private let leftSide: Bool
private weak var originalSuperview: UIView?
private unowned var presentedViewController: UIViewController
private unowned var presentingViewController: UIViewController

Expand Down Expand Up @@ -109,10 +108,6 @@ internal final class SideMenuPresentationController {

presentingViewController.view.isUserInteractionEnabled = config.presentingViewControllerUserInteractionEnabled
containerView.backgroundColor = config.presentationStyle.backgroundColor

originalSuperview = presentingViewController.view.superview
containerView.addSubview(presentingViewController.view)
containerView.addSubview(presentedViewController.view)

layerViews()

Expand Down Expand Up @@ -184,7 +179,6 @@ internal final class SideMenuPresentationController {
}

statusBarView?.removeFromSuperview()
presentedViewController.view.removeFromSuperview()

removeStyles(from: presentingViewController.containerViewController.view)

Expand All @@ -193,7 +187,6 @@ internal final class SideMenuPresentationController {
topNavigationController.interactivePopGestureRecognizer?.isEnabled = interactivePopGestureRecognizerEnabled
}

originalSuperview?.addSubview(presentingViewController.view)
presentingViewController.view.isUserInteractionEnabled = true
config.presentationStyle.dismissalTransitionDidEnd(to: presentedViewController, from: presentingViewController, completed)
}
Expand Down

0 comments on commit 8ecc8ab

Please sign in to comment.