【问题标题】:Right click in UIKit for Mac右键单击 UIKit for Mac
【发布时间】:2019-06-20 09:37:57
【问题描述】:

我有一个 iPad 应用程序,我很容易移植到 Mac。

我正在使用触摸开始/移动/结束,它的端口很好,但我想在 Mac 上的 iOS 应用上使用右键单击。

如何在 UIKit for Mac App 中注册右键?

【问题讨论】:

    标签: ios uikitformac


    【解决方案1】:

    我不认为您可以添加任意右键单击手势,但如果您只想右键单击以显示上下文菜单,那么新的UIContextMenuInteraction 可以满足您的需求:

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

    ... 然后实现UIContextMenuInteractionDelegate 方法来配置和显示上下文菜单。在 macOS 上,“上下文菜单交互”是右键单击,上下文菜单将显示为标准的 macOS 弹出菜单。

    UIContextMenuInteraction 目前的文档很少,所以如果您需要,这篇博文会很有帮助: https://kylebashour.com/posts/ios-13-context-menus

    【讨论】:

      【解决方案2】:

      基于 Adam 的回答,如果您只想在视图上捕获右键单击,则以下操作将起作用:

      class Bubble: UIView, UIContextMenuInteractionDelegate {
          init() {
              super.init(frame: CGRect.zero)
              addInteraction(UIContextMenuInteraction(delegate: self))
          }
      
      // UIContextMenuInteractionDelegate ================================================================
          public func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
              // Do Stuff
              return nil
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-11
        • 2020-01-27
        • 2016-01-20
        • 2012-11-06
        • 1970-01-01
        • 2011-01-11
        • 2020-11-21
        • 1970-01-01
        相关资源
        最近更新 更多