2012-11-12 51 views
1

我的问题很简单。CAGradientLayer violet instead of red

我想使用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]; 
} 

结果,我可以看到:

enter image description here

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

谢谢!干杯:)

回答

4

必须使用的部分,没有完整的数字:

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

当然,要警惕整数除法。在这种情况下,这个混蛋总是能让我受益匪浅。

+1

+1对于实际上我要发布的内容... !!!但是现在已经足够了 – Kamarshad

+1

不错,非常感谢! :D – EdRbt

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图表。

here it is...!!!

here is another good link at where you can get quite good RGB for Red

我希望这可以帮助你在你需要什么.. !!!

+0

不错的提示,但我更喜欢使用分数来提高可读性。使用精确分数是否有优势? – Mazyod

相关问题