如何动态重新着色CGGradientRef

时间:2010-05-21 23:08:12

标签: iphone objective-c core-graphics

我有三个CGGradientRef,我需要能够动态重新着色。当我第一次获得预期结果时初始化CGGradientRef时,但每次我尝试更改颜色时都没有任何反应。为什么呢?

gradient是CALayer的子类中的实例变量:

@interface GradientLayer : CALayer 
{
    CGGradientRef gradient;
    //other stuff   
}
@end

代码:

    if (gradient != NULL)
    {
        CGGradientRelease(gradient);
        gradient = NULL;
    }

    RGBA color[360];
    //set up array
    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
    gradient = CGGradientCreateWithColorComponents
    (
    rgb, 
    color,
    NULL,
    sizeof (color) / sizeof (color[0])
    );
    CGColorSpaceRelease(rgb);
    [self setNeedsDisplay];

1 个答案:

答案 0 :(得分:0)

CGGradientRefs无法动态着色。要使用CGShadingRef动态着色渐变。