Skip to content

Instantly share code, notes, and snippets.

@robertmryan
Last active December 18, 2016 05:45
Show Gist options
  • Save robertmryan/b7a180aac09dcfc3b07c202cd89d786f to your computer and use it in GitHub Desktop.
Save robertmryan/b7a180aac09dcfc3b07c202cd89d786f to your computer and use it in GitHub Desktop.
import Cocoa
@IBDesignable
class HolyView: NSView {
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let center1 = NSPoint(x: bounds.size.width * 0.3 + bounds.origin.x, y: bounds.size.height / 2 + bounds.origin.y)
let radius1 = bounds.size.width / 6
let center2 = NSPoint(x: bounds.size.width * 0.7 + bounds.origin.x, y: bounds.size.height / 2 + bounds.origin.y)
let radius2 = bounds.size.width / 6
let path = NSBezierPath()
path.move(to: NSPoint(x: center1.x, y: center1.y - radius1))
path.appendArc(withCenter: center1, radius: radius1, startAngle: -90, endAngle: 270)
path.move(to: NSPoint(x: center2.x, y: center2.y - radius2))
path.appendArc(withCenter: center2, radius: radius2, startAngle: -90, endAngle: 270)
path.move(to: bounds.origin)
path.appendRect(NSInsetRect(bounds, 10, 10))
path.close()
path.windingRule = .evenOddWindingRule
NSColor.blue.setFill()
path.fill()
NSColor.black.setStroke()
path.lineWidth = 5
path.stroke()
}
}
@robertmryan
Copy link
Author

This illustrates move(to:) to avoid lines between the various subpaths:

screenshot 2016-12-17 21 44 15

Clearly, you're probably not drawing the paths, so it's not necessary, but it's useful when drawing boundaries like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment