iOS 的自定义渐变背景视图不起作用

发布于 2024-11-17 19:32:51 字数 1311 浏览 2 评论 0原文

我有以下 UIView 自定义类:

@implementation BackgroundView

- (void)drawRect:(CGRect)rect 
{

    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    CGGradientRef glossGradient;
    CGColorSpaceRef rgbColorspace;
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };

    float r = (255.0)/(255.0);
    float g = (165.0)/(255.0);
    float b = (0.0)/(255.0);

    float rr = (238.0)/(255.0);
    float gg = (118.0)/(255.0);
    float bb = (0.0)/(255.0);

    CGFloat components[8] = { r, g, b, 1.0,  // Start color
        rr, gg, bb, 1.0 }; // End color

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

    CGRect currentBounds = self.bounds;
    CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
    CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));
    CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0);

    CGGradientRelease(glossGradient);
    CGColorSpaceRelease(rgbColorspace); 
}


@end

基本上,它应该制作橙色渐变。值“r”、“g”和“b”代表浅橙色,值“rr”、“gg”、“bb”代表深橙色。但这不起作用。只有框架的顶部是橙色的,框架的底部是黑色的。我似乎无法摆脱黑色。

我将使用什么值来获得橙色的漂亮渐变。有人可以找一下错误吗?我找不到任何。非常感谢!

I have the following UIView custom class:

@implementation BackgroundView

- (void)drawRect:(CGRect)rect 
{

    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    CGGradientRef glossGradient;
    CGColorSpaceRef rgbColorspace;
    size_t num_locations = 2;
    CGFloat locations[2] = { 0.0, 1.0 };

    float r = (255.0)/(255.0);
    float g = (165.0)/(255.0);
    float b = (0.0)/(255.0);

    float rr = (238.0)/(255.0);
    float gg = (118.0)/(255.0);
    float bb = (0.0)/(255.0);

    CGFloat components[8] = { r, g, b, 1.0,  // Start color
        rr, gg, bb, 1.0 }; // End color

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

    CGRect currentBounds = self.bounds;
    CGPoint topCenter = CGPointMake(CGRectGetMidX(currentBounds), 0.0f);
    CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));
    CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, 0);

    CGGradientRelease(glossGradient);
    CGColorSpaceRelease(rgbColorspace); 
}


@end

Basically, it's supposed to make an orange gradient. The values "r", "g", and "b" are for a light orange, and the values "rr", "gg", "bb" are for a dark orange. But it doesn't work. Only the top of the frame is orange, and the bottom of the frame is black. I can't seem to get rid of the black.

What values would I use for an orange nice gradient. Can someone please look for mistakes? I can't find any. Thanks so much!

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

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

发布评论

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

评论(1

┈┾☆殇 2024-11-24 19:32:51

这是因为您指定渐变的终点位于 currentBounds 的中间 - 请参阅此行:

CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));

要消除黑色,请确保 y 坐标位于视图的底部。

或者,您可以使用此代码在终点之后扩展渐变

 CGContextDrawLinearGradient(currentContext, glossGradient, topCenter, midCenter, kCGGradientDrawsAfterEndLocation);

That's because you specify that the end point for your gradient is in middle of the currentBounds - see this line:

CGPoint midCenter = CGPointMake(CGRectGetMidX(currentBounds), CGRectGetMidY(currentBounds));

To get rid of the black make sure the y coordinate is at the bottom of your view.

Alternatively, you could use this code to extend your gradient after the end point

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