【问题标题】:gradient in CGContextAddArc?CGContextAddArc 中的渐变?
【发布时间】:2011-01-12 08:48:11
【问题描述】:

我正在使用以下代码在 DrawRect 中绘制弧线...我只想绘制带有渐变效果的弧线...请帮忙?

              CGContextSetAlpha(ctx, 0.5);
            CGContextSetRGBFillColor(ctx, color.red, color.green, color.blue, color.alpha );
            CGContextMoveToPoint(ctx, cX, cY);
            CGContextAddArc(ctx, cX, cY, radious+10, (startDeg-90)*M_PI/180.0, (endDeg-90)*M_PI/180.0, 0);
            CGContextClosePath(ctx);
            CGContextFillPath(ctx);

【问题讨论】:

    标签: iphone ipad quartz-2d cgcontext


    【解决方案1】:

    首先创建一个圆弧,然后使用以下代码对路径应用渐变效果,

    CGGradientRef glossGradient;
    CGColorSpaceRef rgbColorspace;
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };
    CGFloat components[8] = { 0.965, 0.965, 0.965, 1.0, // Start color
        0.603, 0.603, 0.603, 1.0 }; // End color
    
    rgbColorspace = CGColorSpaceCreateDeviceRGB();
    glossGradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);
    
    CGRect currentBounds = self.bounds;
    CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
    CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMaxY(currentBounds));
    CGContextDrawLinearGradient(UIGraphicsGetCurrentContext(), glossGradient, topCenter, midCenter, 0);
    
    CGGradientRelease(glossGradient);
    CGColorSpaceRelease(rgbColorspace); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      • 2022-01-19
      • 2016-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多