使用nsbezierpath addClip仅限制用于剪辑的路径内部。我想做相反的事情 - 只在外面画。
- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *dontDrawInThis = ...;
//We want an opposite mask of [dontDrawInThis setClip];
//Drawing comes here
}
这是我的解决方案:
- (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,用cgrect替换nsRect。
@avishic的答案的迅速版本
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()
}