效果图:
A11591D8D8EDEFE7A2CC94CEBE775F04.jpg
代码如下:
UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 200, [[UIScreen mainScreen] bounds].size.width, 400)];
[self.view addSubview:backView];
//初始化CAGradientlayer对象,使它的大小为UIView的大小
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = backView.bounds;
//将CAGradientlayer对象添加在我们要设置背景色的视图的layer层
[backView.layer addSublayer:gradientLayer];
//设置渐变区域的起始和终止位置(范围为0-1)
//设置(1,0)为横向,设置(0,1)为纵向,设置(1,1)为对角方向
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(1, 0);
//设置颜色数组
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor, (__bridge id)[UIColor yellowColor].CGColor];
//设置颜色分割点(范围:0-1)
gradientLayer.locations = @[@(0.0f), @(1.0f)];
网友评论