2013-10-31 194 views
1

我有一个简单的绘图程序,允许用户在屏幕上绘制两个封闭的椭圆。我想用渐变填充椭圆,使用内部椭圆来表示渐变的“百分比”。即Th梯度将在外部椭圆到内部椭圆之间平滑过渡。Quartz2d渐变填充椭圆

我有交互式绘图工作正常,现在我只需要填充渐变。

有什么想法?文档只是谈论完美的圆形渐变,而不是椭圆。

enter image description here

_mike

回答

5

我不知道是否有可能椭圆形的渐变。但是你可以把圆圈变成椭圆形。这个想法是在变换后的坐标系中绘制一个圆。

示例代码:

Result of running code:

- (void)drawRect:(CGRect)rect 
{ 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 
    CGContextScaleCTM(context, 1.0, 0.5); 
    CGGradientRef gradient; 
    CGColorSpaceRef colorspace; 
    CGFloat locations[2] = { 0.0, 1.0}; 
    NSArray *colors = @[(id)[UIColor whiteColor].CGColor, (id)[UIColor blueColor].CGColor]; 
    colorspace = CGColorSpaceCreateDeviceRGB(); 
    gradient = CGGradientCreateWithColors(colorspace, (CFArrayRef)colors, locations); 
    CGPoint startPoint, endPoint; 
    CGFloat startRadius, endRadius; 
    startPoint.x = 180; 
    startPoint.y = 180; 
    endPoint.x = 180; 
    endPoint.y = 180; 
    startRadius = 0; 
    endRadius = 100; 
    CGContextDrawRadialGradient (context, gradient, startPoint, startRadius, endPoint, endRadius, 0); 
    CGContextRestoreGState(context); 
} 

的运行代码结果