问题
Using NSBezierPath addClip only limits drawing to inside the path used for clipping. I'd like to do the opposite - Only draw outside.
- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *dontDrawInThis = ...;
//We want an opposite mask of [dontDrawInThis setClip];
//Drawing comes here
}
回答1:
This was my solution:
- (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
}
For iOS replace NSRect with CGRect.
回答2:
Swift version of @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()
}
来源:https://stackoverflow.com/questions/41219816/using-nsbezierpath-addclip-how-to-invert-clip