iPhone:无法控制绘图渐变的宽度?

时间:2011-07-16 21:24:03

标签: iphone objective-c draw gradients

我需要绘制一个填充渐变的方框。我正在使用UIView并覆盖-drawRect方法。

这是我的代码(简化):

CGContextRef c = UIGraphicsGetCurrentContext();     
CGFloat components[8] = {158.0/255.0,36.0/255.0,134.0/255.0,1.0,115.0/255.0,26.0/255.0,93.0/255.0,1.0};

        CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();

        CGFloat locations[2] = {0.0,1.0};
        CGGradientRef glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, 2);

        CGPoint topCenter = CGPointMake(10, 0);
        CGPoint bottomCenter = CGPointMake(10, 10);
        CGContextDrawLinearGradient(c, glossGradient, topCenter, bottomCenter, 0);

目前的渐变填充填充了整个视图宽度!我该怎么做才能控制我正在绘制的渐变填充框的宽度?

1 个答案:

答案 0 :(得分:0)

没关系..找到答案。我需要剪辑我的CGContextRef以匹配我想要的CGRect:

CGContextClipToRect(c, currentBounds);