gpt4 book ai didi

ios - iPadOS:防止 UIContextMenuInteraction 在不使用指针时触发

转载 作者:行者123 更新时间:2023-12-01 15:35:45 27 4
gpt4 key购买 nike

UIMenu对比 UIContextMenuInteraction对比 UIPointerInteraction
我正在尝试设置 UIContextMenuInteraction以与文件或页面应用程序相同的方式:

  • (长)点击空白处的任意位置显示黑色水平UIMenu
  • 二级(右键/控制)用指针单击空白处的任何位置显示上下文菜单

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

    我可以设置 UIContextMenuInteraction并在其 UIContextMenuInteractionDelegate返回 UIContextMenuConfiguration我想展示的项目。

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

    但是,我无法阻止 UIContextMenuInteraction从触发和显示 UITargetedPreview当长按 View 时,现在似乎有识别不同的方法 UITouchType s 提供给 UIContextMenuInteractionDelegate 的信息.

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



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

    Files.app ui menu and context menu interaction

    最佳答案

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

    要隐藏预览,只需从 previewProvider 返回 nil在 UIContextMenuConfiguration 的初始值设定项中。

    这是一个以 View Controller 的 View 为目标的完整实现:

    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
    }
    }

    关于ios - iPadOS:防止 UIContextMenuInteraction 在不使用指针时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61915466/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com