1

我的问题很简单。

我想使用 CAGradientLayer 为我的 UIView 制作漂亮的渐变背景。

但问题是它显示出丑陋的紫罗兰色,而不是我想要的美妙的红色。

这是我正在使用的代码:

- (void)viewWillAppear:(BOOL)animated {
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = self.view.bounds;
    gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blackColor] CGColor], (id)[[UIColor blackColor] CGColor], (id)[[UIColor colorWithRed:107 green:0 blue:27 alpha:1] CGColor], nil];
    [self.view.layer insertSublayer:gradient atIndex:0];
}

结果,我可以看到:

在此处输入图像描述

这不是我想要的......如果有人可以帮助我。

谢谢 !干杯:)

4

2 回答 2

4

您必须使用分数,而不是整数:

[UIColor colorWithRed:107/255.f green:0.f blue:27/255.f alpha:1.f];

当然,要小心整数除法。那个混蛋总是让我陷入这种情况。

于 2012-11-12T16:18:28.133 回答
1

在这里你应该使用来自 RGB 的颜色作为

-(void)viewWillAppear:(BOOL)animated {
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = self.view.bounds;
//here just set the colour form RGB.
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:exactFractionVal green:exactFractionVal blue:exactFractionval alpha:1.f]; CGColor];
[self.view.layer insertSublayer:gradient atIndex:0];
}

//here exactFractionVal is the float value you should pass there for exact RGB.

如果您在制作 RGB 时遇到任何问题,您可以在许多网站上找到它。在这里您可以看到用于制作所需颜色的 RGB 图表。

这里是...!!!

这是另一个很好的链接,您可以在其中获得非常好的红色 RGB

我希望这可以帮助你在你所需要的..!!!

于 2012-11-12T16:25:34.487 回答