【问题标题】:Using NSBezierPath addClip - How to invert clip使用 NSBezierPath addClip - 如何反转剪辑
【发布时间】:2017-05-04 08:11:09
【问题描述】:

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

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

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

    //Drawing comes here
}

【问题讨论】:

    标签: objective-c macos mask clip nsbezierpath


    【解决方案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。

    【讨论】:

    • bezierPathByReversingPath 在 Xaramin 的 iOS 部分。
    • 非常感谢!这真的帮助了我。我不确定为什么只使用反向路径不起作用,请参阅我对 swift 版本的回答。此外,此答案不包括任何 Xamarin 调用,我的也不包括。
    【解决方案2】:

    @avishic's answer 的 Swift 版本

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-31
      • 2021-08-17
      • 2021-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-18
      相关资源
      最近更新 更多