使用NSBezierPath addClip仅将绘图限制在用于裁剪的路径内。我想做相反的事情 - 只能在外面画画。
- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *dontDrawInThis = ...;
//We want an opposite mask of [dontDrawInThis setClip];
//Drawing comes here
}
答案 0 :(得分:1)
这是我的解决方案:
- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *dontDrawInThis = ...;
// The mask is the whole bounds rect, subtracted dontDrawInThis
NSBezierPath *clip = [NSBezierPath bezierPathWithRect:self.bounds];
[clip appendBezierPath:dontDrawInThis.bezierPathByReversingPath];
[clip setClip];
//Drawing comes here
}
对于iOS,将NSRect替换为CGRect。
答案 1 :(得分:0)
@avishic's answer的快速版本
override func draw(_ dirtyRect: NSRect) {
super.draw(dirtyRect)
let restrictedPath = NSBezierPath()
// fill the restricted path with shapes/paths you want transparent...
let fullRect = NSBezierPath(rect: self.bounds)
fullRect.append(restrictedPath.reversed)
fullRect.setClip()
NSColor.blue.setFill()
frame.fill()
}