Quartz:无法使用渐变填充创建圆角矩形

时间:2011-09-04 16:16:31

标签: iphone objective-c cocoa-touch core-graphics

我正要开始撕掉我的头发。我正在尝试创建一个覆盖圆角区域的渐变填充。这是我drawRect:中的代码:

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);

CGFloat c = BUBBLE_INSET + BUBBLE_CORNER_RADIUS;

CGContextMoveToPoint(ctx, BUBBLE_INSET, c);

CGContextAddArcToPoint(ctx, BUBBLE_INSET, BUBBLE_INSET, c, BUBBLE_INSET, BUBBLE_CORNER_RADIUS);    
CGContextAddLineToPoint(ctx, rect.size.width - c, BUBBLE_INSET);
CGContextAddArcToPoint(ctx, rect.size.width - BUBBLE_INSET, BUBBLE_INSET, rect.size.width - BUBBLE_INSET, c, BUBBLE_CORNER_RADIUS);
CGContextAddLineToPoint(ctx, rect.size.width - BUBBLE_INSET, rect.size.height - c);
CGContextAddArcToPoint(ctx, rect.size.width - BUBBLE_INSET, rect.size.height - BUBBLE_INSET, rect.size.width - c, rect.size.height - BUBBLE_INSET, BUBBLE_CORNER_RADIUS);
CGContextAddLineToPoint(ctx, c, rect.size.height - BUBBLE_INSET);
CGContextAddArcToPoint(ctx, BUBBLE_INSET, rect.size.height - BUBBLE_INSET, BUBBLE_INSET, rect.size.height - c, BUBBLE_CORNER_RADIUS);

CGContextClosePath(ctx);

CGContextClip(ctx);

CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
NSArray *colors = [NSArray arrayWithObjects:color1, color2, nil];

CGGradientRef gradient = CGGradientCreateWithColors(space, (CFArrayRef)colors, NULL);
CGColorSpaceRelease(space);

CGPoint p1 = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint p2 = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

CGContextDrawLinearGradient(ctx, gradient, p1, p2, 0);
CGGradientRelease(gradient);

此代码绘制一个空的白色矩形。路径构建正确(我尝试调用CGContextFillPath()并且结果很好地填充了纯色),color1color2值是正确的(在调试器中看到)。这段代码出了什么问题?

2 个答案:

答案 0 :(得分:2)

我不确定你的颜色是如何包裹颜色数组的。

尝试使用

CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);

代替。

CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextClearRect(ctx, rect);

CGFloat c = BUBBLE_INSET + BUBBLE_CORNER_RADIUS;

CGContextMoveToPoint(ctx, BUBBLE_INSET, c);

CGContextAddArcToPoint(ctx, BUBBLE_INSET, BUBBLE_INSET, c, BUBBLE_INSET, BUBBLE_CORNER_RADIUS);    
CGContextAddLineToPoint(ctx, rect.size.width - c, BUBBLE_INSET);
CGContextAddArcToPoint(ctx, rect.size.width - BUBBLE_INSET, BUBBLE_INSET, rect.size.width - BUBBLE_INSET, c, BUBBLE_CORNER_RADIUS);
CGContextAddLineToPoint(ctx, rect.size.width - BUBBLE_INSET, rect.size.height - c);
CGContextAddArcToPoint(ctx, rect.size.width - BUBBLE_INSET, rect.size.height - BUBBLE_INSET, rect.size.width - c, rect.size.height - BUBBLE_INSET, BUBBLE_CORNER_RADIUS);
CGContextAddLineToPoint(ctx, c, rect.size.height - BUBBLE_INSET);
CGContextAddArcToPoint(ctx, BUBBLE_INSET, rect.size.height - BUBBLE_INSET, BUBBLE_INSET, rect.size.height - c, BUBBLE_CORNER_RADIUS);

CGContextClosePath(ctx);

CGContextClip(ctx);

CGGradientRef gradient;
CGColorSpaceRef rgbColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 1.0, 0.0, 0.0, 1.0,  // Start color
    1.0, 1.0, 1.0, 0.65 }; // End color

rgbColorspace = CGColorSpaceCreateDeviceRGB();
gradient = CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);

CGPoint p1 = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint p2 = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));

CGContextDrawLinearGradient(ctx, gradient, p1, p2, 0);
CGGradientRelease(gradient);

你会得到这个:

偏移量为5.0 半径为10.0

enter image description here

答案 1 :(得分:0)

事实证明,color1和color2的类型为UIColor,这是完全错误的。改变了行

NSArray *colors = [NSArray arrayWithObjects:color1, color2, nil];

NSArray *colors = [NSArray arrayWithObjects:[color1 CGColor], [color2 CGColor], nil];

得到了我期待的结果。