Heestand XYZ
Heestand XYZ

Reputation: 2221

Right click in UIKit for Mac

I've got an iPad app that I ported to Mac really easily.

I'm using touch began / moved / ended, and it ports nice, tho I'd like to use right click on my iOS app on Mac.

How to register right clicks in a UIKit for Mac App?

Upvotes: 3

Views: 1518

Answers (2)

aepryus
aepryus

Reputation: 4825

Building on Adam's answer, if all you want is to capture right clicks on a view the following will work:

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

Upvotes: 3

Adam
Adam

Reputation: 5105

I don’t think you can add arbitrary right-click gestures, but if all you want is for a right-click to show a context menu, then the new UIContextMenuInteraction has got you covered:

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

… and then implement the UIContextMenuInteractionDelegate methods to configure and show the context menu. On macOS, the “context menu interaction” is a right-click, and the context menu will appear as a standard macOS popup menu.

UIContextMenuInteraction is pretty low on documentation at the moment, so this blog post is helpful if you need it: https://kylebashour.com/posts/ios-13-context-menus

Upvotes: 5

Related Questions