我需要绘制一个填充渐变的方框。我正在使用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);
目前的渐变填充填充了整个视图宽度!我该怎么做才能控制我正在绘制的渐变填充框的宽度?
答案 0 :(得分:0)
没关系..找到答案。我需要剪辑我的CGContextRef以匹配我想要的CGRect:
CGContextClipToRect(c, currentBounds);