tbi*_*tbi 1 core-animation ios
我正在尝试在视图上设置渐变背景,但下面的代码使UIView显示为纯黑色.如果我将whiteColor更改为greenColor,则会正确显示渐变.将图层的backgroundColor更改为greenColor会使视图显示为纯绿色.
我的猜测是我正在处理某种透明度问题,但我无法解决这个问题.
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = [NSArray arrayWithObjects:
(id)[[UIColor whiteColor] CGColor],
(id)[[UIColor blackColor] CGColor], nil];
gradientLayer.frame = CGRectMake(0, 0, 1, someView.frame.size.height);
UIGraphicsBeginImageContext(CGSizeMake(1, someView.frame.size.height));
[gradientLayer renderInContext: UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
someView.backgroundColor = [UIColor colorWithPatternImage: image];
Run Code Online (Sandbox Code Playgroud)
您的白色和黑色颜色处于灰度色彩空间.尝试在RGB色彩空间中制作它们:
gradientLayer.colors = @[
(id)[UIColor colorWithRed:0 green:0 blue:0 alpha:1].CGColor,
(id)[UIColor colorWithRed:1 green:1 blue:1 alpha:1].CGColor
];
Run Code Online (Sandbox Code Playgroud)
这应该可以解决你的问题,虽然我不确定这背后的Quartz机制.