1

I have one big CGRect and two small CGRect inside. I want to draw the big CGRect in red and to form two transparent holes corresponding to the small CGRect.

I am not able to do it. I have tried to use NSBezierPath but in macOS there is no method NSBezierPath.CGPath like in UIBezierPath for iOS.

Nisba
  • 3,210
  • 2
  • 27
  • 46

1 Answers1

1

You don't have to use Core Graphics. You can create a NSView subclass and just stroke/fill the path in draw(_:). In 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()
    }

}

You can then add that view programmatically, or you can make it @IBDesignable and add it directly on your storyboard.

enter image description here

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Two things: 1) how can path "know" where to be drawn, I mean I expected to pass it the reference of a `cgContext` or something else. 2) now I try implementing this solution, is it possible to work with layers too? – Nisba Dec 17 '16 at 20:12
  • You just add it to your view hierarchy. Do you just want to show the view, or is there some reason why you need to use CoreGraphics' `cgContext`? – Rob Dec 17 '16 at 20:13
  • The even/odd rule winding rule would fill the intersection with the fill color. If you don't want it to do that, you'd have to define a path for the intersecting circles (e.g. not two full circles, but two partial arcs whose start and end angles are chosen carefully to stop where the other picks up; some basic trigonometry is needed, but it's not hard). – Rob Dec 17 '16 at 20:19
  • Thank you. I realised that this answers, that solves my question, does not fit for my original problem. So I will be more detailed in another answer. For the moment I mark this as the best question! – Nisba Dec 17 '16 at 20:23
  • Here the question @Rob http://stackoverflow.com/questions/41202823/filter-on-calayer-except-for-a-shape-which-is-an-union-of-non-necessarily-disti – Nisba Dec 17 '16 at 20:38
  • In your comment, you ask whether you can use layers. Sure, use `CAShapeLayer`, but I'd build a `CGMutablePath` directly rather than building a `NSBezierPath` and then creating a `CGMutablePath` from that. – Rob Dec 17 '16 at 21:00
  • One more thing: I think that in order to create the "holes" the line `path.move(to: .zero)` in your question before the edit is a key point. Can you explain better how to tell Xcode that we want to get an hole instead that to add a figure? – Nisba Dec 17 '16 at 22:48
  • No, that `move(to: .zero)` is not needed. I just like to move to the point in case you're drawing the path. See https://gist.github.com/robertmryan/b7a180aac09dcfc3b07c202cd89d786f. But you're not stroking the path, presumably, just filling it, so it's academic. That's why I deleted the path related code, as it wasn't relevant to the broader question and it seemed like we could get lost in irrelevant stuff like this. – Rob Dec 18 '16 at 05:46