小编tbi*_*tbi的帖子

CAGradientLayer显示为纯色

我正在尝试在视图上设置渐变背景,但下面的代码使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)

core-animation ios

1
推荐指数
1
解决办法
5143
查看次数

标签 统计

core-animation ×1

ios ×1