【发布时间】:2011-03-27 10:23:16
【问题描述】:
我正在尝试在视图上设置渐变背景,但我下面的代码使 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];
【问题讨论】:
标签: ios core-animation