【问题标题】:Right click on UIButton for UIKitForMac (catalyst)右键单击 UIKitForMac 的 UIButton(催化剂)
【发布时间】:2020-12-11 08:25:24
【问题描述】:

是否可以在 UIKitForMac 中轻松启用对 Catalyst 应用的右键单击?

目前以下代码在左键单击时完美运行,但在右键单击时不会调用任何内容:

button.addTarget(self, action: #selector(doSomething), for: .touchUpInside)

// 左键调用而不是右键调用

@objc func doSomething(sender:UIButton, event:UIEvent) -> Bool {

【问题讨论】:

    标签: swift uibutton catalyst uikitformac


    【解决方案1】:

    https://developer.apple.com/design/human-interface-guidelines/ios/controls/context-menus/

    此菜单通过右键单击 Mac Catalyst 来工作

    1.添加迭代

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

    2。添加扩展 UIContextMenuIteractionDelegate

    extension AccountViewVC: UIContextMenuInteractionDelegate {
    
     public func contextMenuInteraction(_ interaction: UIContextMenuInteraction, configurationForMenuAtLocation location: CGPoint) -> UIContextMenuConfiguration? {
          
          return UIContextMenuConfiguration(identifier: nil, previewProvider: nil, actionProvider: { suggestedActions in
               let delete = UIAction(title: "Delete", image: UIImage(systemName: "arrow")) { action in
                    
               }
               let children = [delete]
               return UIMenu(title: "Main Menu", children: children)
          })
     }}
     
    

    【讨论】:

    • 这几乎是解决方案,但是如果您有一个 UIButton,仍然会调用 touchUpInside 事件,这意味着您将获得一个右键单击事件和一个左键单击..
    • 奇怪,我刚检查过,当我右键单击时没有为我调用 touchUpInside 事件
    • 是的,在 macOS 上右击按预期工作,但是在 iOS 上,长按触发上下文菜单也调用 touchUpInside。
    • 使用targetEnvironment区分#if targetEnvironment(macCatalyst)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 2016-09-08
    • 2020-01-05
    • 2020-12-14
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多