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 0dc2b54..1e168d8 100644 --- a/Sources/SideNavigationController.swift +++ b/Sources/SideNavigationController.swift @@ -31,6 +31,9 @@ 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! { willSet(newValue) { @@ -116,6 +119,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 } @@ -159,6 +167,7 @@ open class SideNavigationController: UIViewController { if let viewController = viewController { viewController.view.removeFromSuperview() viewController.removeFromParent() + delegate = nil } } @@ -219,11 +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) @@ -250,10 +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) @@ -309,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! @@ -337,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 { @@ -505,3 +519,12 @@ fileprivate extension SideNavigationController { self.closeSide() } } + +public protocol SideNavigationControllerDelegate { + + func sideWillClose() + func sideDidClosed() + func sideWillOpen() + func sideDidOpen() + +}