From 0cf8e3a7784221df5947e4d7034a4aebf2ea35ea Mon Sep 17 00:00:00 2001 From: Marcus Wu Date: Tue, 26 Nov 2019 15:51:56 +0800 Subject: [PATCH] [#44] Add API to disable tap-to-dismiss --- ContextMenu/ContextMenu.swift | 19 +++++++++++++++++++ ContextMenu/ContextMenuDelegate.swift | 1 + .../ContextMenuPresentationController.swift | 4 ++++ 3 files changed, 24 insertions(+) diff --git a/ContextMenu/ContextMenu.swift b/ContextMenu/ContextMenu.swift index b6caaf6..f320943 100644 --- a/ContextMenu/ContextMenu.swift +++ b/ContextMenu/ContextMenu.swift @@ -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() {} @@ -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) + } } diff --git a/ContextMenu/ContextMenuDelegate.swift b/ContextMenu/ContextMenuDelegate.swift index 915afae..bc3c4b4 100644 --- a/ContextMenu/ContextMenuDelegate.swift +++ b/ContextMenu/ContextMenuDelegate.swift @@ -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) } diff --git a/ContextMenu/ContextMenuPresentationController.swift b/ContextMenu/ContextMenuPresentationController.swift index 59a61d3..c76105e 100644 --- a/ContextMenu/ContextMenuPresentationController.swift +++ b/ContextMenu/ContextMenuPresentationController.swift @@ -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