Jon*_*ier 5 cocoa drawing gradient compositing
我试图用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];
}
Run Code Online (Sandbox Code Playgroud)
小智 2
是的。这是我们用来执行此操作的代码示例。它使用源图像,并且在比例因子方面有一些有趣的业务,但是您应该能够使用基本结构和合成选择来完成您需要的操作。(此代码位于 [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];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2005 次 |
| 最近记录: |