0

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

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

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

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

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

1 回答 1

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)
      })
 }}
 
于 2020-08-24T11:50:43.630 回答