Cocoa:使用 NSGradient 作为在其下方绘图的遮罩

发布于 2024-09-07 10:56:52 字数 1131 浏览 2 评论 0原文

我正在尝试使用 Cocoa 绘制形状的反射。我已经应用了 NSAffineTransform 并成功重新绘制了形状,但现在我不知道如何在其上绘制 alpha 蒙版。我正在使用 NSCompositeDestinationOut 操作,但它给了我一个不需要的结果:alt text http://img687.imageshack.us/img687/2417/capturedcran20100623094.png

我不太确定如何解决这个问题 - 我需要让它这样渐变才起作用作为 alpha 遮罩,实际上并不显示。我使用了错误的合成模式吗?

谢谢!如果需要的话,这是渐变代码:

- (void)fadeOutRect:(NSRect)rect {
    [NSGraphicsContext saveGraphicsState];
    [[NSGraphicsContext currentContext] setCompositingOperation:NSCompositeDestinationOut];

    NSGradient *gradient = [[NSGradient alloc] initWithColorsAndLocations:
                            [[NSColor blackColor] colorWithAlphaComponent:0.5], 0.0,
                            [[NSColor blackColor] colorWithAlphaComponent:1.0], 0.8, nil];
    [gradient drawInRect:NSMakeRect(rect.origin.x, rect.origin.y + rect.size.height - ( PILL_HEIGHT * 2 ),
                                    rect.size.width, PILL_HEIGHT) angle:270];

    [NSGraphicsContext restoreGraphicsState];
}

I am trying to draw a shape's reflection using Cocoa. I have already applied an NSAffineTransform and redrawn the shape successfully, but now I can't figure out how to draw an alpha mask over it. I'm using an NSCompositeDestinationOut operation, but it's giving me an unwanted result:alt text http://img687.imageshack.us/img687/2417/capturedcran20100623094.png

I'm not exactly sure how to fix this - I need to make it so the gradient acts only as an alpha mask and is not actually displayed. Am I using the wrong compositing mode?

Thanks! Here's the gradient code if needed:

- (void)fadeOutRect:(NSRect)rect {
    [NSGraphicsContext saveGraphicsState];
    [[NSGraphicsContext currentContext] setCompositingOperation:NSCompositeDestinationOut];

    NSGradient *gradient = [[NSGradient alloc] initWithColorsAndLocations:
                            [[NSColor blackColor] colorWithAlphaComponent:0.5], 0.0,
                            [[NSColor blackColor] colorWithAlphaComponent:1.0], 0.8, nil];
    [gradient drawInRect:NSMakeRect(rect.origin.x, rect.origin.y + rect.size.height - ( PILL_HEIGHT * 2 ),
                                    rect.size.width, PILL_HEIGHT) angle:270];

    [NSGraphicsContext restoreGraphicsState];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

机场等船 2024-09-14 10:56:54

是的。这是我们用来执行此操作的代码示例。它使用源图像,并且在比例因子方面有一些有趣的业务,但是您应该能够使用基本结构和合成选择来完成您需要的操作。 (此代码位于 [reflectionImage lockFocus] 块内, self 是我们要对其进行反射的 NSImage。)

// Draw our mask into the image
NSGradient* fade = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.5]
                                                 endingColor:[NSColor clearColor]];
[fade drawFromPoint:NSMakePoint(0.0, size.height)
            toPoint:NSMakePoint(0.0, 0.0)
            options:0];

// Composite the original image, upside-down
NSAffineTransform* flipper = [NSAffineTransform transform];
[flipper scaleXBy:1.0 yBy:-1.0];
[flipper concat];
[self drawInRect:NSMakeRect(0.0, -1.0*size.height, size.width, size.height)
        fromRect:NSMakeRect(0.0, 0.0, self.size.width, size.height / scaleFactor)
       operation:NSCompositeSourceIn fraction:1.0];

Yep. Here's an example of the code we use to do this. It uses a source image, and has a bit of funny business with a scale factor, but you should be able to use the basic structure and compositing choices to do what you need. (This code is within the [reflectionImage lockFocus] block, and self is the NSImage that we're making the reflection of.)

// Draw our mask into the image
NSGradient* fade = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.5]
                                                 endingColor:[NSColor clearColor]];
[fade drawFromPoint:NSMakePoint(0.0, size.height)
            toPoint:NSMakePoint(0.0, 0.0)
            options:0];

// Composite the original image, upside-down
NSAffineTransform* flipper = [NSAffineTransform transform];
[flipper scaleXBy:1.0 yBy:-1.0];
[flipper concat];
[self drawInRect:NSMakeRect(0.0, -1.0*size.height, size.width, size.height)
        fromRect:NSMakeRect(0.0, 0.0, self.size.width, size.height / scaleFactor)
       operation:NSCompositeSourceIn fraction:1.0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文