簡體   English   中英

右鍵單擊 UIButton for UIKitForMac (catalyst)

[英]Right click on UIButton for UIKitForMac (catalyst)

是否可以在 UIKitForMac 中輕松啟用右鍵單擊 Catalyst 應用程序?

目前,以下代碼在左鍵單擊時可以完美運行,但在右鍵單擊時不會調用任何內容:

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

// 調用左鍵單擊但不調用右鍵單擊

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM