如何绘制径向渐变扇区(iphone)

时间:2011-07-14 15:44:00

标签: iphone objective-c core-graphics

如何在目标c(核心图形)中绘制具有径向渐变的扇区(实心圆弧) 我用

CGContextDrawRadialGradient

但它画圈。 如果你说我如何通过径向渐变填充任何形状将是很好的。感谢

1 个答案:

答案 0 :(得分:16)

添加当前上下文的路径,该路径定义所需的形状并在绘制前剪切上下文。示例代码:

- (void)drawRect:(CGRect)rect
{
    CGPoint c = self.center ;
    // Drawing code
    CGContextRef cx = UIGraphicsGetCurrentContext();

    CGContextSaveGState(cx);
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();

    CGFloat comps[] = {1.0,1.0,0.0,1.0,0.0,1.0,0.0,1.0};
    CGFloat locs[] = {0,1};
    CGGradientRef g = CGGradientCreateWithColorComponents(space, comps, locs, 2);


    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, c.x, c.y);
    CGPathAddLineToPoint(path, NULL, c.x, c.y-100);
    CGPathAddArcToPoint(path, NULL, c.x+100, c.y-100, c.x+100, c.y, 100);
    CGPathAddLineToPoint(path, NULL, c.x, c.y);

    CGContextAddPath(cx, path);
    CGContextClip(cx);

    CGContextDrawRadialGradient(cx, g, c, 1.0f, c, 320.0f, 0);

    CGContextRestoreGState(cx);   
    ... // Do some more drawing may be
}