石英:无法创建带渐变填充的圆角矩形
问题描述:
我正要开始撕掉我的头发。我正在尝试创建一个包含圆角矩形区域的渐变填充。这是我在我的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()
,结果很好地用纯色填充),color1
和color2
值是正确的(在调试器中看到)。这段代码有什么问题?
答
原来颜色1和颜色2是UIColor
类型,这是完全错误的。改了行
NSArray *colors = [NSArray arrayWithObjects:color1, color2, nil];
到
NSArray *colors = [NSArray arrayWithObjects:[color1 CGColor], [color2 CGColor], nil];
,并得到了结果,我期待。
答
我不确定你的颜色是如何包裹颜色数组的。
使用
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
你确定,color1和color2是在RGB-Space中定义的。即[[UIColor blackColor] CGColor]返回单色间隔颜色。 – vikingosegundo
原来,color1和color2是UIColor类型,这是完全错误的。在需要的地方添加'CGColor'调用,现在正在工作 –
这可能会让人感到困惑。 – vikingosegundo