作為前端開發(fā),我們經(jīng)常會遇到產(chǎn)品要求我們給圖片添加漸變色,這樣界面看起來就不是那么單調(diào),下面就主要談一下使用CAGradientLayer實現(xiàn)某些漸變的特效鳄袍。
首先看一下效果圖:
? ?效果一:
效果二:
效果三:
效果一:
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);//結(jié)束點
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);// 結(jié)束點
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;
// 貝塞爾曲線(創(chuàng)建一個圓)
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0)
radius:radius
startAngle:startAngle
endAngle:endAngle
clockwise:clockwise];
// 獲取path
layer.path = path.CGPath;
layer.position = center;
// 設(shè)置填充顏色為透明
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);
// 結(jié)束點
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軸上的點。至于每個顏色所占區(qū)域和漸變分割線是由locations上的點到(startPoint與endPoint這條直線)所確定確定樱哼。
下載地址:demo下載github地址