1、创建CAShapeLayer图层
self.firstLayer =[CAShapeLayer layer];
self.firstLayer.fillColor =[UIColor clearColor].CGColor;
self.firstLayer.frame = self.showView.bounds;
self.firstLayer.lineWidth = 10;
self.firstLayer.strokeColor =[UIColor grayColor].CGColor;
[self.showView.layer addSublayer:self.firstLayer];
self.secondLayer =[CAShapeLayer layer];
self.secondLayer.fillColor =[UIColor clearColor].CGColor;
self.secondLayer.frame = self.showView.bounds;
//要画的图层的strokeColor一定不能设置为clearcolor,否则画不出来
self.secondLayer.strokeColor =[UIColor greenColor].CGColor;
self.secondLayer.lineWidth = 5;
2、创建贝塞尔路径,也就是要动态画的路径
UIBezierPath *path= [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.showView.bounds.size.width/2, self.showView.bounds.size.height/2) radius:self.showView.bounds.size.width/2-2 startAngle:-M_PI_2 endAngle:M_PI *1.5 clockwise:YES];
self.firstLayer.path = path.CGPath; //设置创建的路径的path为该路径
self.secondLayer.strokeEnd = 0; //设置渲染路径,做动画实际就是改变strokeEnd的值,取值范围为[0 1];
3、创建渐变图层
CALayer *gradientLayer=[CALayer layer]; //该图层为渐变涂层的容器
self.gradientLayerLeft =[CAGradientLayer layer];
self.gradientLayerLeft.frame = CGRectMake(0, 0, self.showView.bounds.size.width/2, self.showView.bounds.size.height);
self.gradientLayerLeft.colors =@[(__bridge id)[UIColor redColor].CGColor,(__bridge id)[UIColor yellowColor].CGColor,(__bridge id)[UIColor blueColor].CGColor];
self.gradientLayerLeft.locations = @[@0,@0.5,@1];
self.gradientLayerLeft.startPoint = CGPointMake(0, 0);
self.gradientLayerLeft.endPoint = CGPointMake(0, 1);
[gradientLayer addSublayer:self.gradientLayerLeft];
self.gradientLayerRight =[CAGradientLayer layer];
self.gradientLayerRight.frame = CGRectMake(self.showView.bounds.size.width/2, 0, self.showView.bounds.size.width/2, self.showView.bounds.size.height);
self.gradientLayerRight.colors =@[(__bridge id)[UIColor greenColor].CGColor,(__bridge id)[UIColor purpleColor].CGColor,(__bridge id)[UIColor blackColor].CGColor];
self.gradientLayerRight.locations = @[@0,@0.5,@1];
self.gradientLayerRight.startPoint = CGPointMake(0, 0);
self.gradientLayerRight.endPoint = CGPointMake(0, 1);
[gradientLayer addSublayer:self.gradientLayerRight];
//以上是为容器图层添加了两个渐变涂层
4、设置添加到涂层
//设置mask,把渐变的合成视图添加到容器view的layer中,其中self.showView.layer为容器view的layer, 让后把要画的涂层设置为渐变涂层的mask图层
[self.showView.layer addSublayer:gradientLayer];
[gradientLayer setMask:self.secondLayer]; //mask的作用就是显示实体