diff --git a/BSAnimateListLeftSideMenuExample/Base.lproj/Main.storyboard b/BSAnimateListLeftSideMenuExample/Base.lproj/Main.storyboard index 3ad6a48..886681c 100644 --- a/BSAnimateListLeftSideMenuExample/Base.lproj/Main.storyboard +++ b/BSAnimateListLeftSideMenuExample/Base.lproj/Main.storyboard @@ -28,7 +28,7 @@ - + @@ -44,53 +44,73 @@ - + - + + + + + + + - + @@ -118,7 +138,10 @@ - + + + + diff --git a/BSAnimateListLeftSideMenuExample/ViewController.swift b/BSAnimateListLeftSideMenuExample/ViewController.swift index 09a3968..8201116 100644 --- a/BSAnimateListLeftSideMenuExample/ViewController.swift +++ b/BSAnimateListLeftSideMenuExample/ViewController.swift @@ -26,13 +26,15 @@ class ViewController: UIViewController { guard let vc = storyboard?.instantiateViewController(withIdentifier: "MenuVC") as? MenuViewController else { return } SideMenuManager.shared.sideMenuViewController = vc navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(self.show(_:))) - SideMenuManager.shared.addScreenEdgeGesture(to: self) SideMenuManager.shared.views = views } func setupUI() { + title = "BSLeftSideMenu" view.backgroundColor = .white + navigationController?.navigationBar.tintColor = .black + navigationController?.navigationBar.isTranslucent = false firstButton.addTarget(self, action: #selector(self.dismissSM(_:)), for: .touchUpInside) firstLabel.text = "First Label" diff --git a/Sources/SideMenuAnimatedTransitioning.swift b/Sources/SideMenuAnimatedTransitioning.swift index 4b42b96..e86adb3 100644 --- a/Sources/SideMenuAnimatedTransitioning.swift +++ b/Sources/SideMenuAnimatedTransitioning.swift @@ -19,7 +19,10 @@ class SideMenuAnimatedTransitioning: NSObject, UIViewControllerAnimatedTransitio } } - func finishAnimate() { + func finishAnimate(isReversed: Bool = false) { + animators.forEach { + $0.isReversed = isReversed + } animators.forEach { $0.continueAnimation(withTimingParameters: nil, durationFactor: 0) } } diff --git a/Sources/SideMenuManager.swift b/Sources/SideMenuManager.swift index d4ac5ac..d5a7ce3 100644 --- a/Sources/SideMenuManager.swift +++ b/Sources/SideMenuManager.swift @@ -7,12 +7,12 @@ import UIKit -public class SideMenuManager { +public final class SideMenuManager { public static let shared = SideMenuManager() public var configurations: SettingsModel = SideMenuSettingsModel() - var sideMenuViewController: (SideMenuViewControllerProtocol & UIViewController)? + public var sideMenuViewController: (SideMenuViewControllerProtocol & UIViewController)? private var sideMenuNavigationController: UINavigationController? var transitionController: SideMenuTransitionController? = SideMenuTransitionController() var isPresenting: Bool = false @@ -21,8 +21,8 @@ public class SideMenuManager { } // MARK: Gesture -public extension SideMenuManager { - func addScreenEdgeGesture(to viewController: UIViewController) { +extension SideMenuManager { + public func addScreenEdgeGesture(to viewController: UIViewController) { let gesture = UIScreenEdgePanGestureRecognizer(target: self, action: #selector(self.handleGesture(_:))) gesture.edges = .left @@ -30,7 +30,7 @@ public extension SideMenuManager { if currentViewController != viewController { currentViewController = viewController } } - @objc func handleGesture(_ gestureRecognizer: UIPanGestureRecognizer) { + @objc private func handleGesture(_ gestureRecognizer: UIPanGestureRecognizer) { switch gestureRecognizer.state { case .began: show(from: currentViewController!, views: views) @@ -42,7 +42,13 @@ public extension SideMenuManager { let progress = distance / width transitionController?.animationController?.animate(with: progress) case .ended: - transitionController?.animationController?.finishAnimate() + let width = configurations.menuWidth + let distance = gestureRecognizer.translation(in: gestureRecognizer.view?.superview).x + let progress = distance / width + transitionController?.animationController?.finishAnimate(isReversed: progress < 0.5) + if progress < 0.5 { + sideMenuNavigationController?.dismiss(animated: false, completion: { self.isPresenting = false }) + } transitionController?.animationController?.isInteraction = false default: return } diff --git a/Sources/SideMenuNavigationViewController.swift b/Sources/SideMenuNavigationViewController.swift index dd26f4b..437a015 100644 --- a/Sources/SideMenuNavigationViewController.swift +++ b/Sources/SideMenuNavigationViewController.swift @@ -8,7 +8,7 @@ import UIKit class SideMenuNavigationViewController: UINavigationController { - override var transitioningDelegate: UIViewControllerTransitioningDelegate? { + override var transitioningDelegate: UIViewControllerTransitioningDelegate? { get { SideMenuManager.shared.transitionController } set {} }