Skip to content

Commit

Permalink
[GitHawkApp#44] Add API to disable tap-to-dismiss
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Wu committed Nov 26, 2019
1 parent 846252c commit 0cf8e3a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
19 changes: 19 additions & 0 deletions ContextMenu/ContextMenu.swift
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 0cf8e3a

Please sign in to comment.