使用CAGradientLayer實(shí)現(xiàn)顏色漸變和特效

作為前端開發(fā),我們經(jīng)常會(huì)遇到產(chǎn)品要求我們給圖片添加漸變色,這樣界面看起來就不是那么單調(diào),下面就主要談一下使用CAGradientLayer實(shí)現(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);//起始點(diǎn)

colorLayer.endPoint = CGPointMake(1.0, 1.0);//結(jié)束點(diǎn)

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)];// 起始點(diǎn)

colorLayer.startPoint = CGPointMake(0, 0);// 結(jié)束點(diǎn)

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)建一個(gè)圓)

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)];

// 起始點(diǎn)

colorLayer.startPoint = CGPointMake(0, 0);

// 結(jié)束點(diǎn)

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的坐標(biāo)系,范圍為(0,1)。locations里面的值是遞增的,其位置點(diǎn)可以看做是y值為0在x軸上的點(diǎn)。至于每個(gè)顏色所占區(qū)域和漸變分割線是由locations上的點(diǎn)到(startPoint與endPoint這條直線)所確定確定。

下載地址:demo下載github地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • >復(fù)雜的組織都是專門化的 >Catharine R. Stimpson 到目前為止,我們已經(jīng)探討過`CALayer...
    夜空下最亮的亮點(diǎn)閱讀 1,222評(píng)論 0 2
  • iOS 繪制顏色漸變圓環(huán) 本文主要介紹一種繪制顏色漸變的進(jìn)度圓環(huán). 先上效果圖: 實(shí)現(xiàn)思路: CAShapeLay...
    DanDanC閱讀 3,317評(píng)論 2 3
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺iOS動(dòng)畫全貌。在這里你可以看...
    F麥子閱讀 5,268評(píng)論 5 13
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫效果,實(shí)現(xiàn)這些動(dòng)畫的過程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,690評(píng)論 6 30
  • Whenever rain drops leave The sky will be still blue But ...
    劉小木閱讀 184評(píng)論 0 0

友情鏈接更多精彩內(nèi)容