Using NSBezierPath addClip - How to invert clip

别等时光非礼了梦想. 提交于 2019-12-11 04:24:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!