CAGradientLayer
官方说明:
The gradient layer draws a color gradient over its background color,filling the shape of the layer (i.e. including rounded corners).
具体代码:
UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 113)];
[self.view addSubview:topView];
topView.backgroundColor = [UIColor lightGrayColor];
// 添加颜色渐变效果
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
// colors :由哪几种颜色进行渐变
gradientLayer.colors =
@[(__bridge id)[UIColor hexStringToColor:@"FFCA37"].CGColor,
(__bridge id)[UIColor hexStringToColor:@"FFb142"].CGColor,
(__bridge id)[UIColor hexStringToColor:@"F7981F"].CGColor];
//locations:颜色开始渐变的位置,取值区间是[0,1]
gradientLayer.locations = @[@0.2,@0.5,@0.8];
//
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(1.0, 0);
// 为gradientLayer设置frame
gradientLayer.frame = topView.bounds;
[topView.layer addSublayer:gradientLayer];