Hi, Is it possible to change the anchorpoint of NSView if I don't use layer? If not how can I use layer and still draw my notch?
import Cocoa
import Combine
class NotchView: NSView {
private var mouseDownSubject = PassthroughSubject<NSPoint, Never>()
var mouseDownPublisher: AnyPublisher<NSPoint, Never> {
return mouseDownSubject.eraseToAnyPublisher()
}
//Draw notch
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
drawNotch()
}
func drawNotch () {
let size = self.frame.size
let r: CGFloat = 16.0
let gap: CGFloat = 1.0
let notch = NSBezierPath()
notch.move(to: NSPoint(x: 0.0, y: size.height))
notch.curve(to: NSPoint(x: r, y: size.height - r),
controlPoint1: NSPoint(x: r - gap, y: size.height),
controlPoint2: NSPoint(x: r, y: size.height - gap))
notch.line(to: NSPoint(x: r, y: r))
notch.curve(to: NSPoint(x: 2 * r, y: 0.0),
controlPoint1: NSPoint(x: r, y: gap),
controlPoint2: NSPoint(x: r + gap, y: 0.0))
notch.line(to: NSPoint(x: size.width - 2 * r, y: 0.0))
notch.curve(to: NSPoint(x: size.width - r, y: r),
controlPoint1: NSPoint(x: size.width - r - gap, y: 0.0),
controlPoint2: NSPoint(x: size.width - r, y: gap))
notch.line(to: NSPoint(x: size.width - r, y: size.height - r))
notch.curve(to: NSPoint(x: size.width, y: size.height),
controlPoint1: NSPoint(x: size.width - r, y: size.height - gap),
controlPoint2: NSPoint(x: size.width - r + gap, y: size.height))
notch.close()
NSColor.systemPink.setFill() //change to black to see the notch
notch.fill()
}
//Tap with mouse
override func mouseDown(with event: NSEvent) {
super.mouseDown(with: event)
//move notch to top
mouseDownSubject.send(event.locationInWindow)
}
}