Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#44] Add API to disable tap-to-dismiss #53

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions ContextMenu/ContextMenu.swift
Expand Up @@ -15,6 +15,16 @@ public class ContextMenu: NSObject {
public static let shared = ContextMenu()

var item: Item?

public var delegate: ContextMenuDelegate? {
get {
self.item?.delegate
}

set {
self.item?.delegate = newValue
}
}

private override init() {}

Expand Down Expand Up @@ -55,5 +65,14 @@ public class ContextMenu: NSObject {
item.viewController.modalPresentationCapturesStatusBarAppearance = true
sourceViewController.present(item.viewController, animated: true)
}

/// Dismiss current showing context menu.
///
/// - Parameters:
/// - animated: Animated the transition.
/// - completion: The block to execute after the view controller is dismissed.
public func dismiss(animated: Bool = true, completion: (() -> Void)? = nil) {
self.item?.viewController.dismiss(animated: animated, completion: completion)
}

}
1 change: 1 addition & 0 deletions ContextMenu/ContextMenuDelegate.swift
Expand Up @@ -9,6 +9,7 @@
import UIKit

public protocol ContextMenuDelegate: class {
func contextMenuAllowTapToClose() -> Bool
func contextMenuWillDismiss(viewController: UIViewController, animated: Bool)
func contextMenuDidDismiss(viewController: UIViewController, animated: Bool)
}
4 changes: 4 additions & 0 deletions ContextMenu/ContextMenuPresentationController.swift
Expand Up @@ -170,6 +170,10 @@ class ContextMenuPresentationController: UIPresentationController {
}

@objc func onTap(recognizer: UITapGestureRecognizer) {
if let delegate = item.delegate, delegate.contextMenuAllowTapToClose() == false {
return
}

guard recognizer.state == .ended,
let containerView = self.containerView,
presentedView?.frame.contains(recognizer.location(in: containerView)) == false
Expand Down