CAGradientLayer
可以用来处理渐变色的图层结构,也可以用来做隐式动画,大部分时间和CAShapeLayer配和使用
相关属性
常用属性
NSArray colors 颜色分配 至少要2种颜色
NSArray<NSNumber *>locations 颜色分割线,颜色之间的界限
CGPoint startPoint 颜色起点
CGPoint endPoint 颜色结束点
demo
渐变色环形进度条
@property(nonatomic,strong)UIView *contentMaksView; 遮罩层
@property(nonatomic,strong) CAGradientLayer *gradlayer; 渐变
@property(nonatomic,strong) NSTimer *waterTimer; 定时器
@property(nonatomic,strong) CAShapeLayer *shapeLayer; 绘制
//贝塞尔曲线路径画圆
UIBezierPath *bezi = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10,10,100,100)];
//创建绘制对象
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
//获取路径
layer.path = bezi.CGPath;
//路径颜色
layer.strokeColor = [UIColor redColor].CGColor;
//填充色
layer.fillColor = [UIColor whiteColor].CGColor;
//线宽
layer.lineWidth = 20;
//开始值
layer.strokeStart = 0;
//结束值
layer.strokeEnd = 0;
//默认不设置是实线绘制,如果设置那么会虚线绘制前一个值是绘制虚线粗细,后一个值间隔粗细
layer.lineDashPattern = @[@6,@2];
self.shapeLayer = layer;
//渐变图层
_gradlayer = [CAGradientLayer layer];
[ _gradlayer setStartPoint:CGPointMake(0.3, 0.5)];
[ _gradlayer setEndPoint:CGPointMake(1, 1)];
_gradlayer.frame = CGRectMake(0,0,frame.size.width, frame.size.height);
_gradlayer.colors=@[(id)[UIColor redColor].CGColor,(id)[UIColor yellowColor].CGColor,(id)[UIColor brownColor].CGColor,(id)[UIColor purpleColor].CGColor];
[ _gradlayer setLocations:@[@0.2,@0.4,@0.7,@1.0]];
//渐变图层上添加一个遮挡图层,在渐变图层上显示遮挡图层形状,
[_gradlayer setMask:_shapeLayer];
//将渐变图层加入到view的layer上
[self.layer addSublayer:_gradlayer];
//遮挡图层
_contentMaksView = [[UIView alloc]initWithFrame:CGRectMake(9,9,102,102)];
_contentMaksView.layer.cornerRadius = 50;
_contentMaksView.layer.masksToBounds = YES;
_contentMaksView.backgroundColor = [UIColor whiteColor];
[self addSubview:_contentMaksView];
//定时器
self.waterTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(waterAction) userInfo:nil repeats:YES];
- (void)waterAction{
if (self.shapeLayer.strokeEnd >=1) {
[self.waterTimer invalidate];
self.waterTimer = nil;
return;
}else{
self.shapeLayer.strokeEnd +=0.02;
}
}
效果图