gpt4 book ai didi

iphone - quartz : can't create a rounded rectangle with gradient fill

转载 作者:行者123 更新时间:2023-12-03 21:05:40 25 4
gpt4 key购买 nike

我正要开始扯掉我的头发。我正在尝试创建一个渐变填充,覆盖带有圆角的矩形区域。这是我的 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 值是正确的(在调试器中看到)。这段代码有什么问题?

最佳答案

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

尝试使用

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

关于iphone - quartz : can't create a rounded rectangle with gradient fill,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7300514/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com