实现渐变色通常会用图片代替,本文利用CAGradientLayer类绘制渐变色
// 创建view
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, 200)];
view.layer.cornerRadius = 10;
view.layer.masksToBounds = YES;
[self.view addSubview:view];
//初始化CAGradientlayer对象,使它的大小为UIView的大小
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame = view.bounds;
//将CAGradientlayer对象添加在我们要设置背景色的视图的layer层
[view.layer addSublayer:gradientLayer];
//设置渐变区域的起始和终止位置(范围为0-1)startPoint & endPoint设置为(0,0)(1.0,0)代表水平方向渐变,(0,0)(0,1.0)代表竖直方向渐变
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(1, 0);
//设置颜色数组
gradientLayer.colors = @[(__bridge id)[UIColor blueColor].CGColor,
(__bridge id)[UIColor redColor].CGColor];
//设置颜色分割点(范围:0-1)
gradientLayer.locations = @[@(0.0f), @(1.0f)];
屏幕快照 2017-12-21 下午4.44.48.png
网友评论