Quartz2D 和渐变线

发布于 2024-08-29 01:06:13 字数 24 浏览 6 评论 0原文

如何用quartz2d绘制渐变线?

how can i draw gradient line with quartz2d?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ˉ厌 2024-09-05 01:06:13

CGContextDrawLinearGradient 绘制渐变。使用CGContextClipToRect(或相关的剪切函数)剪切到要填充渐变的区域。

不支持用渐变描边或填充路径,而是剪辑到要填充的区域。

CGContextDrawLinearGradient draws gradients. Use CGContextClipToRect (or related clipping functions) to clip to the region you want to fill with a gradient.

There's no support for stroking or filling a path with a gradient, you instead clip to the region you want to fill.

じ违心 2024-09-05 01:06:13

我在 drawRect: 中使用了它,效果非常好:

double slope, cosy, siny;
double halfWidth = 2; // the thickness of the line will be 2*halfWith

// the start and end points have to be inside UIView frame coordinates
CGPoint start = CGPointMake(30, 20);
CGPoint end = CGPointMake(80, 90);  

slope = atan2((start.y - end.y), (start.x - end.x));
cosy = cos(slope);
siny = sin(slope);

CGContextRef context = UIGraphicsGetCurrentContext();

CGGradientRef myGradient;
CGColorSpaceRef myColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 1.0, 1.0, 1.0, 1.0,  // Start color (White)
                          1.0, 0.0, 0.0, 1.0 }; // End color (Red)

myColorspace = CGColorSpaceCreateDeviceRGB();
myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);

CGContextMoveToPoint(context, start.x -halfWidth*siny , start.y +halfWidth*cosy);
CGContextAddLineToPoint(context, end.x -halfWidth*siny , end.y +halfWidth*cosy  );
CGContextAddLineToPoint(context, end.x +halfWidth*siny , end.y -halfWidth*cosy );
CGContextAddLineToPoint(context, start.x +halfWidth*siny, start.y -halfWidth*cosy);
CGContextAddLineToPoint(context, start.x -halfWidth*siny , start.y +halfWidth*cosy);    
CGContextClip(context);

CGContextDrawLinearGradient (context, myGradient, start, end, 0);
CGColorSpaceRelease( myColorspace );
CGGradientRelease (myGradient);

I used this inside drawRect: and worked like a charm:

double slope, cosy, siny;
double halfWidth = 2; // the thickness of the line will be 2*halfWith

// the start and end points have to be inside UIView frame coordinates
CGPoint start = CGPointMake(30, 20);
CGPoint end = CGPointMake(80, 90);  

slope = atan2((start.y - end.y), (start.x - end.x));
cosy = cos(slope);
siny = sin(slope);

CGContextRef context = UIGraphicsGetCurrentContext();

CGGradientRef myGradient;
CGColorSpaceRef myColorspace;
size_t num_locations = 2;
CGFloat locations[2] = { 0.0, 1.0 };
CGFloat components[8] = { 1.0, 1.0, 1.0, 1.0,  // Start color (White)
                          1.0, 0.0, 0.0, 1.0 }; // End color (Red)

myColorspace = CGColorSpaceCreateDeviceRGB();
myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);

CGContextMoveToPoint(context, start.x -halfWidth*siny , start.y +halfWidth*cosy);
CGContextAddLineToPoint(context, end.x -halfWidth*siny , end.y +halfWidth*cosy  );
CGContextAddLineToPoint(context, end.x +halfWidth*siny , end.y -halfWidth*cosy );
CGContextAddLineToPoint(context, start.x +halfWidth*siny, start.y -halfWidth*cosy);
CGContextAddLineToPoint(context, start.x -halfWidth*siny , start.y +halfWidth*cosy);    
CGContextClip(context);

CGContextDrawLinearGradient (context, myGradient, start, end, 0);
CGColorSpaceRelease( myColorspace );
CGGradientRelease (myGradient);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文