首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Quartz:无法创建具有渐变填充的圆角矩形

Quartz:无法创建具有渐变填充的圆角矩形
EN

Stack Overflow用户
提问于 2011-09-05 00:16:31
回答 2查看 902关注 0票数 2

我都快把头发扯下来了。我正在尝试创建一个渐变填充覆盖区域的矩形圆角。这是我的drawRect:中的代码

代码语言:javascript
复制
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);

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-06 07:30:46

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

代码语言:javascript
复制
NSArray *colors = [NSArray arrayWithObjects:color1, color2, nil];

代码语言:javascript
复制
NSArray *colors = [NSArray arrayWithObjects:[color1 CGColor], [color2 CGColor], nil];

并且得到了我所期望的结果。

票数 0
EN

Stack Overflow用户

发布于 2011-09-05 02:12:31

我不确定您的颜色是如何包装在颜色数组中的。

尝试使用

代码语言:javascript
复制
CGGradientCreateWithColorComponents(rgbColorspace, components, locations, num_locations);

而不是。

代码语言:javascript
复制
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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7300514

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档