4

UIMenu对比UIContextMenuInteraction对比UIPointerInteraction

UIContextMenuInteraction我正在尝试以与“文件”或“页面”应用程序相同的方式进行设置:

  • (长)点击空白处任意位置显示黑色横线UIMenu
  • 使用指针在空白区域的任意位置单击辅助(右键/控制)显示上下文菜单

请参阅下面附加的 GIF 上的演示。

我能够设置UIContextMenuInteractionUIContextMenuInteractionDelegate返回UIContextMenuConfiguration我想要显示的项目。

小黑也是一样UIMenu,我可以使用UILongPressGestureRecognizer并显示菜单UIMenuController.shared.showMenu

但是,我无法阻止在长按视图时UIContextMenuInteraction触发和显示,UITargetedPreview现在似乎有一种方法可以通过UITouchType提供给UIContextMenuInteractionDelegate.

我也找不到如何以编程方式显示上下文菜单,没有UIContextMenuInteraction. 有没有办法做到这一点?

问题

这在 Files.app 中是如何实现的?

Files.app ui 菜单和上下文菜单交互

4

1 回答 1

1

没有办法以编程方式触发上下文菜单,但是通过一些简单的簿记,您可以阻止它在不需要时显示(例如,当您的响应者上的触摸处于活动状态时)。

要隐藏预览,只需从previewProviderUIContextMenuConfiguration 的初始化程序中返回 nil。

这是一个以视图控制器的视图为目标的完整实现:

import UIKit

class ViewController: UIViewController {

    var touchesInSession = false

    override func viewDidLoad() {
        super.viewDidLoad()

        let interaction = UIContextMenuInteraction(delegate: self)
        view.addInteraction(interaction)

        let recognizer = UILongPressGestureRecognizer(target: self, action: #selector(longPressHandler))
        view.addGestureRecognizer(recognizer)
    }

    @objc func longPressHandler(recognizer: UILongPressGestureRecognizer) {
        guard recognizer.state == .began else { return }
        presentMenu(from: recognizer.location(in: view))
    }

    func presentMenu(from location: CGPoint) {
        view.becomeFirstResponder()
        let saveMenuItem = UIMenuItem(title: "New Folder", action: #selector(createFolder))
        let deleteMenuItem = UIMenuItem(title: "Get Info", action: #selector(getInfo))
        UIMenuController.shared.menuItems = [saveMenuItem, deleteMenuItem]
        UIMenuController.shared.showMenu(from: view, rect: .init(origin: location, size: .zero))
    }

    @objc func createFolder() {
        print("createFolder")
    }

    @objc func getInfo() {
        print("getInfo")
    }

    // MARK: UIResponder
    override var canBecomeFirstResponder: Bool { true }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        touchesInSession = true
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesEnded(touches, with: event)
        touchesInSession = false
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesCancelled(touches, with: event)
        touchesInSession = false
    }
}

extension ViewController: UIContextMenuInteractionDelegate {
    func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
        guard !touchesInSession else { return nil }
        let configuration = UIContextMenuConfiguration(identifier: "PointOnlyContextMenu" as NSCopying, previewProvider: { nil }, actionProvider: { suggestedActions in
            let newFolder = UIAction(title: "New Folder", image: UIImage(systemName: "folder.badge.plus")) { [weak self] _ in
                self?.createFolder()
            }
            let info = UIAction(title: "Get Info", image: UIImage(systemName: "info.circle")) { [weak self] _ in
                self?.getInfo()
            }
            return UIMenu(title: "", children: [newFolder, info])
        })
        return configuration
    }
}

于 2020-06-06T09:56:33.277 回答