From c63403c42d0fc5b609636b901bf2421e6abf068b Mon Sep 17 00:00:00 2001 From: joseph sagiv Date: Thu, 21 May 2020 18:03:04 +0200 Subject: [PATCH 1/3] Add open/close listener delegate --- Sources/SideNavigationController.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Sources/SideNavigationController.swift b/Sources/SideNavigationController.swift index 0dc2b54..aa875df 100644 --- a/Sources/SideNavigationController.swift +++ b/Sources/SideNavigationController.swift @@ -31,6 +31,8 @@ open class SideNavigationController: UIViewController { public private(set) var left: Side? public private(set) var right: Side? + + public var delegate: SideNavigationControllerDelegate? public var mainViewController: UIViewController! { willSet(newValue) { @@ -159,6 +161,7 @@ open class SideNavigationController: UIViewController { if let viewController = viewController { viewController.view.removeFromSuperview() viewController.removeFromParent() + delegate = nil } } @@ -224,6 +227,7 @@ open class SideNavigationController: UIViewController { side.viewController.view.isHidden = false self.updateSide(with: direction, progress: 0) }, completion: { _ in + self.delegate?.sideDidClosed() side.viewController.view.isHidden = true self.revertSideDirection = false self.mainGestures(enabled: false, direction: direction) @@ -254,6 +258,7 @@ open class SideNavigationController: UIViewController { self.visibleSideViewController = side.viewController self.updateSide(with: direction, progress: 1) }, completion: { _ in + self.delegate?.sideDidOpen() self.revertSideDirection = true self.mainGestures(enabled: true, direction: direction) #if os(iOS) @@ -505,3 +510,11 @@ fileprivate extension SideNavigationController { self.closeSide() } } + +public protocol SideNavigationControllerDelegate { + + func sideDidClosed() + + func sideDidOpen() + +} From 32b0e8f2a24b7be3f355ca03c3e6abeb5b78f43a Mon Sep 17 00:00:00 2001 From: joseph sagiv Date: Mon, 15 Jun 2020 10:11:49 +0200 Subject: [PATCH 2/3] Option to close side when a touch gesture is detected outside --- Sources/SideNavigationController+NestedTypes.swift | 6 +++++- Sources/SideNavigationController.swift | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Sources/SideNavigationController+NestedTypes.swift b/Sources/SideNavigationController+NestedTypes.swift index c4f922f..5b87444 100644 --- a/Sources/SideNavigationController+NestedTypes.swift +++ b/Sources/SideNavigationController+NestedTypes.swift @@ -39,6 +39,7 @@ public extension SideNavigationController { public var panningEnabled: Bool public var scale: CGFloat public var position: Position + public var closeOnTouchOutside: Bool public var shadowColor: UIColor { get { return UIColor(cgColor: self.shadowCGColor) @@ -47,6 +48,7 @@ public extension SideNavigationController { self.shadowCGColor = newValue.cgColor } } + public fileprivate(set) var shadowCGColor: CGColor! public init(widthPercent: CGFloat = 0.33, @@ -58,7 +60,8 @@ public extension SideNavigationController { alwaysInteractionEnabled: Bool = false, panningEnabled: Bool = true, scale: CGFloat = 1, - position: Position = .back) { + position: Position = .back, + closeOnTouchOutside: Bool = true) { self.widthPercent = widthPercent self.animationDuration = animationDuration self.overlayColor = overlayColor @@ -68,6 +71,7 @@ public extension SideNavigationController { self.panningEnabled = panningEnabled self.scale = scale self.position = position + self.closeOnTouchOutside = closeOnTouchOutside self.shadowColor = shadowColor } } diff --git a/Sources/SideNavigationController.swift b/Sources/SideNavigationController.swift index aa875df..938f0f9 100644 --- a/Sources/SideNavigationController.swift +++ b/Sources/SideNavigationController.swift @@ -118,6 +118,11 @@ open class SideNavigationController: UIViewController { self.updateSide(with: direction, progress: 0) #if os(iOS) self.sideGestures(enabled: true) + if side.options.closeOnTouchOutside { + self.overlay.addGestureRecognizer(self.gestures.mainTap) + } else { + self.overlay.removeGestureRecognizer(self.gestures.mainTap) + } #endif } From f3942252a400b56f628cd79f41387c126c74a017 Mon Sep 17 00:00:00 2001 From: joseph sagiv Date: Tue, 18 Aug 2020 12:10:44 +0300 Subject: [PATCH 3/3] Add side will close & open delegate methods --- Sources/SideNavigationController.swift | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Sources/SideNavigationController.swift b/Sources/SideNavigationController.swift index 938f0f9..1e168d8 100644 --- a/Sources/SideNavigationController.swift +++ b/Sources/SideNavigationController.swift @@ -32,6 +32,7 @@ open class SideNavigationController: UIViewController { public private(set) var left: Side? public private(set) var right: Side? + public private(set) var isSideOpen: Bool = false public var delegate: SideNavigationControllerDelegate? public var mainViewController: UIViewController! { @@ -227,12 +228,14 @@ open class SideNavigationController: UIViewController { return } side.viewController.view.endEditing(animated) + self.delegate?.sideWillClose() UIView.animate(withDuration: animated ? side.options.animationDuration : 0, animations: { self.visibleSideViewController = nil side.viewController.view.isHidden = false self.updateSide(with: direction, progress: 0) }, completion: { _ in self.delegate?.sideDidClosed() + self.isSideOpen = false side.viewController.view.isHidden = true self.revertSideDirection = false self.mainGestures(enabled: false, direction: direction) @@ -259,11 +262,13 @@ open class SideNavigationController: UIViewController { return } self.mainViewController.view.endEditing(animated) + self.delegate?.sideWillOpen() UIView.animate(withDuration: animated ? side.options.animationDuration : 0, animations: { self.visibleSideViewController = side.viewController self.updateSide(with: direction, progress: 1) }, completion: { _ in self.delegate?.sideDidOpen() + self.isSideOpen = true self.revertSideDirection = true self.mainGestures(enabled: true, direction: direction) #if os(iOS) @@ -319,7 +324,7 @@ public extension SideNavigationController { fileprivate class Gestures { public static let velocityTolerance: CGFloat = 600 - + private weak var sideNavigationController: SideNavigationController? #if os(iOS) public var leftScreenEdgePan: UIScreenEdgePanGestureRecognizer! @@ -347,14 +352,13 @@ public extension SideNavigationController { #endif self.mainPan = UIPanGestureRecognizer(target: self, action: #selector(handle(panGesture:))) - self.mainTap = UITapGestureRecognizer(target: self, action: #selector(handle(tapGesture:))) #if os(tvOS) self.mainTap.allowedPressTypes = [NSNumber(value: UIPress.PressType.menu.rawValue)] #endif self.mainTap.require(toFail: self.mainPan) } - + @objc private func handle(panGesture: UIPanGestureRecognizer) { guard let sideNavigationController = self.sideNavigationController else { @@ -518,8 +522,9 @@ fileprivate extension SideNavigationController { public protocol SideNavigationControllerDelegate { + func sideWillClose() func sideDidClosed() - + func sideWillOpen() func sideDidOpen() }