如何绘制径向渐变扇区(iphone) [英] How can I draw a sector with radial gradient (iphone)

查看:112
本文介绍了如何绘制径向渐变扇区(iphone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I draw a sector (filled arc) with radial gradient in objective c (Core Graphics) I use

CGContextDrawRadialGradient

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

but it draws circle. It will be good if you say me how to fill any shape by radial gradient. Thanks

推荐答案

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

Add a path to current context that defines the desired shape and clip the context before drawing. Sample code:

- (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
}

这篇关于如何绘制径向渐变扇区(iphone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆