CAGradientLayer是CALayer的一个子类,可以实现渐变色,如图
CAGradientLayer的相关属性:
(1)colors 渐变的颜色
(2)locations 由NSNumber组成的数组,确定所有颜色的梯度范围,NSNumber的值只能在0-1之间,并且值必须是单调递增
(3)startPoint 和 endPoint,颜色绘制的起点坐标和终点坐标,控制渐变色的方向,范围在(0,0)-(1,1)之间
示例:
//设置view1渐变色,横向渐变
- (void)updateView1Color
{
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor yellowColor].CGColor, (__bridge id)[UIColor blueColor].CGColor];
gradientLayer.locations = @[@0.3, @0.7, @1.0];
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(1.0, 0);
gradientLayer.frame = self.view1.bounds;
[self.view1.layer addSublayer:gradientLayer];
}