macOS 画一个矩形,里面有两个洞

macOS draw a rectangle with two holes inside

我里面有一个大CGRect和两个小CGRect。我想把大CGRect画成红色,小CGRect对应两个透明孔

我做不到。我尝试使用 NSBezierPath 但在 macOS 中没有方法 NSBezierPath.CGPath 就像在 UIBezierPath 中用于 iOS.

您不必使用 Core Graphics。您可以创建一个 NSView 子类,只创建 stroke/fill draw(_:) 中的路径。在 Swift 3:

class HolyView: NSView {

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        let path = ... // build the `NSBezierPath` however you want

        NSColor.blue.setFill()
        path.fill()
    }

}

然后您可以通过编程方式添加该视图,或者您可以创建它 @IBDesignable 并将其直接添加到您的情节提要中。