iOS中的四点渐变 [英] Four point gradient in iOS
问题描述
我计划创建一个四点渐变,如下图所示,通过核心图形绘制两个线性渐变,并使用第三个黑白线性渐变在它们之间进行遮罩。
I am planing to create a four point gradient, pictured below, by drawing two linear gradients via core graphics and masking between them with a third black and white linear gradient.
是否有更有效的方法使用核心图形或其他绘制四点渐变?
Is there a more efficient way to draw a four point gradient using core graphics, or other?
推荐答案
您可以在使用时保存遮罩渐变一个CGBlendMode。控制确切的颜色更难。但是如果这对你来说并不重要,那么在代码行方面可能会更有效率,也可能在性能方面更有效。
You can save the mask gradient when you use a CGBlendMode. It's just harder to control the exact colors. But if that's not important for you, it could be a little more efficient in terms of lines of code and maybe also in terms of performance.
这是一个随机的例子colors和CGBlendModeExclusion(CGBlendModeDifference给你类似的效果)
Here's an example with some random colors and CGBlendModeExclusion (CGBlendModeDifference gives you a similar effect)
- (void) drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetBlendMode(ctx, kCGBlendModeExclusion);
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGFloat col1[8] = {
1.0, 0.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0
};
CGGradientRef grad1 = CGGradientCreateWithColorComponents (space, col1, NULL, 2);
CGContextDrawLinearGradient(ctx, grad1, CGPointMake(0, 0), CGPointMake(0, 320), 0);
CGFloat col2[8] = {
1.0, 0.5, 0.0, 1.0,
0.0, 1.0, 0.0, 1.0
};
CGGradientRef grad2 = CGGradientCreateWithColorComponents (space, col2, NULL, 2);
CGContextDrawLinearGradient(ctx, grad2, CGPointMake(0, 0), CGPointMake(320, 0), 0);
CGGradientRelease(grad1);
CGGradientRelease(grad2);
CGColorSpaceRelease(space);
}
这篇关于iOS中的四点渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!