swift – NSRectFill和NSBezierPath(rect)之间有什么区别.fill

前端之家收集整理的这篇文章主要介绍了swift – NSRectFill和NSBezierPath(rect)之间有什么区别.fill前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我知道我可以使用NSRectFill(bounds)填充矩形.但是我想保留PDF输出的透明度,我发现只能使用NSBezierPath(rect:bounds).fill()
这两者有什么不同(幕后)?

func drawBackground() {
    CGContextSaveGState(currentContext)
    if (NSGraphicsContext.currentContextDrawingToScreen()) {
        NSColor(patternImage: checkerboardImage).set()
        NSRectFillUsingOperation(bounds,NSCompositingOperation.CompositeSourceOver)
    }
    NSColor.clearColor().setFill()
    //NSRectFill(bounds) //option 1
    NSBezierPath(rect: bounds).fill() // option 2
    CGContextRestoreGState(currentContext)
}

extension NSImage {

    static func checkerboardImageWithSize(size : CGFloat) -> NSImage {
        let fullRect = NSRect(x: 0,y: 0,width: size,height: size)
        let halfSize : CGFloat = size * 0.5;
        let upperSquareRect = NSRect(x: 0,width: halfSize,height: halfSize);
        let bottomSquareRect = NSRect(x: halfSize,y: halfSize,width:halfSize,height: halfSize);
        let image = NSImage(size: NSSize(width: size,height: size))
        image.lockFocus()
        NSColor.whiteColor()
        NSRectFill(fullRect)
        NSColor(deviceWhite: 0.0,alpha:0.1).set()
        NSRectFill(upperSquareRect)
        NSRectFill(bottomSquareRect)
        image.unlockFocus()
        return image
    }
}

解决方法

我大部分时间都是iOS程序员,而且在AppKit方面这些日子并不是很流畅,但我的猜测是你得到了错误的NSCompositingOperation.我从文档中看到NSRectFill使用NSCompositeCopy.如果您使用NSRectFillUsingOperation,可能会更好地工作,您可以在其中指定合成操作.

猜你在找的cocoa相关文章