Skip to content

Add open/close listener delegate #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Sources/SideNavigationController+NestedTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -47,6 +48,7 @@ public extension SideNavigationController {
self.shadowCGColor = newValue.cgColor
}
}

public fileprivate(set) var shadowCGColor: CGColor!

public init(widthPercent: CGFloat = 0.33,
Expand All @@ -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
Expand All @@ -68,6 +71,7 @@ public extension SideNavigationController {
self.panningEnabled = panningEnabled
self.scale = scale
self.position = position
self.closeOnTouchOutside = closeOnTouchOutside
self.shadowColor = shadowColor
}
}
Expand Down
29 changes: 26 additions & 3 deletions Sources/SideNavigationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -159,6 +167,7 @@ open class SideNavigationController: UIViewController {
if let viewController = viewController {
viewController.view.removeFromSuperview()
viewController.removeFromParent()
delegate = nil
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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!
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -505,3 +519,12 @@ fileprivate extension SideNavigationController {
self.closeSide()
}
}

public protocol SideNavigationControllerDelegate {

func sideWillClose()
func sideDidClosed()
func sideWillOpen()
func sideDidOpen()

}