作为前端开发,我们经常会遇到产品要求我们给图片添加渐变色,这样界面看起来就不是那么单调,下面就主要谈一下使用CAGradientLayer实现某些渐变的特效。
首先看一下效果图:
效果一:
效果二:
效果三:
效果一:
CAGradientLayer *colorLayer = [CAGradientLayer layer];
colorLayer.frame = (CGRect){CGPointZero,CGSizeMake(200, 200)};
colorLayer.position = self.view.center;
[self.view.layer addSublayer:colorLayer];
//颜色分配
colorLayer.colors = @[(__bridge id)[UIColor colorWithRed:0.0/255 green:222.0/255 blue:200.0/255 alpha:1.0].CGColor,(__bridge id)[UIColor colorWithRed:75.0/255 green:255.0/255 blue:210.0/255 alpha:1.0].CGColor,(__bridge id)[UIColor colorWithRed:190.0/255 green:253.0/255 blue:220.0/255 alpha:1.0].CGColor];
colorLayer.startPoint = CGPointMake(0.0, 0.0);//起始点
colorLayer.endPoint = CGPointMake(1.0, 1.0);//结束点
colorLayer.locations = @[@(0.25),@(0.5),@(0.75)];//颜色渐变位置分割线
效果二:
CAGradientLayer *colorLayer = [CAGradientLayer layer];
colorLayer.frame = (CGRect){CGPointZero,CGSizeMake(200, 200)};
colorLayer.position = self.view.center;
[self.view.layer addSublayer:colorLayer];
//颜色分配
colorLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,(__bridge id)[UIColor grayColor].CGColor,(__bridge id)[UIColor yellowColor].CGColor];
colorLayer.locations = @[@(0.25),@(0.5),@(0.75)];
colorLayer.startPoint = CGPointMake(0, 0);
colorLayer.endPoint = CGPointMake(1, 0);
_timer = [NSTimer scheduledTimerWithTimeInterval:0.5 repeats:YES block:^(NSTimer *timer){
static CGFloat length = -0.1f;
if (length >= 1.1) {
length = - 0.1f;
[CATransaction setDisableActions:YES];
colorLayer.locations = @[@(length),@(length + 0.1),@(length + 0.15)];
}else{
[CATransaction setDisableActions:NO];
colorLayer.locations = colorLayer.locations = @[@(length),@(length + 0.1),@(length + 0.15)];
}
length += 0.1f;
}];
[_timer fire];
效果三:
-(void)addCircleGradientLayer{
CAGradientLayer *colorLayer = [CAGradientLayer layer];
colorLayer.backgroundColor = [UIColor orangeColor].CGColor;
colorLayer.frame = CGRectMake(0, 120, 200, 200);
colorLayer.position = CGPointMake(self.view.center.x, colorLayer.frame.origin.y);
[self.view.layer addSublayer:colorLayer];
// 颜色分配
colorLayer.colors = @[(__bridge id)[UIColor blackColor].CGColor,(__bridge id)[UIColor whiteColor].CGColor,(__bridge id)[UIColor blackColor].CGColor];
colorLayer.locations = @[@(-0.2), @(-0.1), @(0)];// 起始点
colorLayer.startPoint = CGPointMake(0, 0);// 结束点
colorLayer.endPoint = CGPointMake(1, 0);
CAShapeLayer *circle = [self createCircleWithCenter:CGPointMake(100, 110) radius:90 startAngle:DEGREES(0) endAngle:DEGREES(360) clockwise:YES lineDashPattern:nil];
circle.strokeColor = [UIColor redColor].CGColor;
[self.view.layer addSublayer:circle];
circle.strokeEnd = 1.0f;
colorLayer.mask = circle;
_timer1 = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer *timer){
static NSInteger index = 1;
if (index ++ % 2 == 0) {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"locations"];
animation.fromValue = @[@(-0.1), @(-0.15), @(0)];
animation.toValue = @[@(1.0), @(1.1), @(1.15)];
animation.duration = 1.0;
[colorLayer addAnimation:animation forKey:nil];}
} ]; [_timer1 fire];}
-(CAShapeLayer *)createCircleWithCenter:(CGPoint)center radius:(CGFloat)radius startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle clockwise:(BOOL)clockwise lineDashPattern:(NSArray *)lineDashPattern
{
CAShapeLayer *layer = [CAShapeLayer layer];
layer.lineCap = kCALineCapRound;
// 贝塞尔曲线(创建一个圆)
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0)
radius:radius
startAngle:startAngle
endAngle:endAngle
clockwise:clockwise];
// 获取path
layer.path = path.CGPath;
layer.position = center;
// 设置填充颜色为透明
layer.fillColor = [UIColor clearColor].CGColor;
// 获取曲线分段的方式
if (lineDashPattern)
{
layer.lineDashPattern = lineDashPattern;
}
return layer;
}
-(void)addRectangleGradientLayer
{
CAGradientLayer *colorLayer = [CAGradientLayer layer];
colorLayer.backgroundColor = [UIColor orangeColor].CGColor;
colorLayer.frame = CGRectMake(0, 300, 300, 100);
colorLayer.position = CGPointMake(self.view.center.x, colorLayer.frame.origin.y);
[self.view.layer addSublayer:colorLayer];
// 颜色分配
colorLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor lightGrayColor].CGColor,
(__bridge id)[UIColor redColor].CGColor];
colorLayer.locations = @[@(-0.2), @(-0.1), @(0)];
// 起始点
colorLayer.startPoint = CGPointMake(0, 0);
// 结束点
colorLayer.endPoint = CGPointMake(1, 0);
_timer2 = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer *timer){
static NSInteger index = 1;
if (index ++ % 2 == 0) {
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"locations"];
animation.fromValue = @[@(-0.1), @(-0.15), @(0)];
animation.toValue = @[@(1.0), @(1.1), @(1.15)];
animation.duration = 1.0;
[colorLayer addAnimation:animation forKey:nil];
}
}];
[_timer2 fire];
}
注:startPoint,endPoint,locations遵循Layler的坐标系,范围为(0,1)。locations里面的值是递增的,其位置点可以看做是y值为0在x轴上的点。至于每个颜色所占区域和渐变分割线是由locations上的点到(startPoint与endPoint这条直线)所确定确定。
下载地址:demo下载github地址