是否可以在UIKitForMac中轻松右键单击Mac 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)
})
}}