使用NSBezierPath addClip - 如何反转剪辑

时间:2016-12-19 09:50:56

标签: objective-c macos mask clip nsbezierpath

使用NSBezierPath addClip仅将绘图限制在用于裁剪的路径内。我想做相反的事情 - 只能在外面画画。

- (void)drawRect:(NSRect)dirtyRect {
    NSBezierPath *dontDrawInThis = ...;

    //We want an opposite mask of [dontDrawInThis setClip];

    //Drawing comes here
}

2 个答案:

答案 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()
}