【问题标题】:iPhone : can't control the width of drawing gradients?iPhone:无法控制绘制渐变的宽度?
【发布时间】:2011-07-16 21:24:02
【问题描述】:

我需要绘制一个填充有渐变的框。我正在使用 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);

目前的渐变填充填充了view的整个宽度!我可以做些什么来控制我正在绘制的渐变填充框的宽度?

【问题讨论】:

    标签: iphone objective-c draw gradient


    【解决方案1】:

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

    CGContextClipToRect(c, currentBounds);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-21
      • 1970-01-01
      • 1970-01-01
      • 2013-06-24
      • 2012-08-14
      • 1970-01-01
      • 2013-05-13
      相关资源
      最近更新 更多