CAGradientLayer简单的理解就是渐变层,图层绘制通过GPU可以加速渲染速度,colors和locations是需要注意的地方,Colors填充的渐变颜色,locations是渐变位置结束的位置;渐变的区域通过startPoint和endPoint控制,采用单位坐标系,位置从0到1,关于渐变层的介绍有很多,先来看一组基础的渐变效果:
基础渐变
最上面的图层渐变
//FlyElephant
CAGradientLayer *gradientLayer = [CAGradientLayer layer];
gradientLayer.frame =CGRectMake(80, 80, 200, 80);
gradientLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor greenColor].CGColor,
(__bridge id)[UIColor yellowColor].CGColor,
(__bridge id)[UIColor blueColor].CGColor];
gradientLayer.locations = @[@(0.2), @(0.4), @(0.6),@(0.8)];
gradientLayer.startPoint = CGPointMake(0, 0);
gradientLayer.endPoint = CGPointMake(0, 1);
[self.view.layer addSublayer:gradientLayer];
第一种渐变比较明显,下面的渐变颜色现金不会像最上面那么夸张:
//FlyElephant
CAGradientLayer *gradientLayer1 = [CAGradientLayer layer];
gradientLayer1.frame =CGRectMake(80, 170, 200, 40);
gradientLayer1.colors = @[(__bridge id)[UIColor redColor].CGColor,(__bridge id)[UIColor purpleColor].CGColor];
gradientLayer1.locations = @[@(0.5),@(1.0)];
gradientLayer1.type = kCAGradientLayerAxial;
gradientLayer1.startPoint = CGPointMake(0, 0);
gradientLayer1.endPoint = CGPointMake(0, 1);
[self.view.layer addSublayer:gradientLayer1];
中间一张诺维茨基的图片其实是加了渐变颜色的,注意分割线第一个点是负数:
self.personImgView.clipsToBounds=YES;
CAGradientLayer *gradientLayer3= [CAGradientLayer layer];
gradientLayer3.frame =self.personImgView.bounds;
gradientLayer3.colors = @[(__bridge id)[UIColor blackColor].CGColor,(__bridge id)[UIColor clearColor].CGColor];
gradientLayer3.locations = @[@(-2),@(1)];
gradientLayer3.type = kCAGradientLayerAxial;
gradientLayer3.startPoint = CGPointMake(0, 0);
gradientLayer3.endPoint = CGPointMake(0, 1);
gradientLayer3.type = kCAGradientLayerAxial;
[self.personImgView.layer addSublayer:gradientLayer3];
估计大家会对最后两种彩色的渐变更有兴趣一点,Web网站和App有的会使用这两种效果做进度条显示,第一种是通过mask控制显示位置,第二种是通过Timer不断的变换Layer的整体的变换颜色,有个小逻辑在里面是
①初始颜色:红色→蓝色→黄色
②颜色切换:黄色→红色→蓝色
self.progressLayer=[CAGradientLayer layer];
self.progressLayer.frame=CGRectMake(0, 500, [UIScreen mainScreen].bounds.size.width, 2);
[self setupProgress:self.progressLayer];
[self.view.layer addSublayer:self.progressLayer];
self.progressGradientLayer=[CAGradientLayer layer];
self.progressGradientLayer.frame=CGRectMake(0, 520, [UIScreen mainScreen].bounds.size.width, 2);
[self setupProgress:self.progressGradientLayer];
[self.view.layer addSublayer:self.progressGradientLayer];
self.progressMaskLayer=[CALayer layer];
self.progressMaskLayer.backgroundColor=[UIColor blackColor].CGColor;
self.progressMaskLayer.frame=CGRectMake(0, 0, 0, 2);
self.timer=[NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];
self.timer.fireDate=[[NSDate date] dateByAddingTimeInterval:1];
self.gradientTimer=[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(progressGradient) userInfo:nil repeats:YES];
self.gradientTimer.fireDate=[[NSDate date] dateByAddingTimeInterval:1];
NSTimer更新:
-(void)setupProgress:(CAGradientLayer *)layer {
layer.startPoint=CGPointMake(0.0, 0.5);
layer.endPoint=CGPointMake(1.0, 0.5);
NSMutableArray *colors = [NSMutableArray array];
for (NSInteger hue = 0; hue <= 360; hue += 5)
{
UIColor * color = [UIColor colorWithHue:1.0 * hue / 360
saturation:1.0
brightness:1.0
alpha:1.0];
[colors addObject:(id)[color CGColor]];
}
layer.colors=[NSArray arrayWithArray:colors];
}
-(void)updateProgress {
self.progress+=0.1;
if (self.progress>=1) {
[self.timer invalidate];
return;
}
CGRect frame=self.progressLayer.bounds;
frame.size.width=frame.size.width*self.progress;
self.progressMaskLayer.frame=frame;
self.progressLayer.mask=self.progressMaskLayer;
}
-(void)progressGradient {
NSMutableArray * colorArray = [[self.progressGradientLayer colors] mutableCopy];
UIColor * lastColor = [colorArray lastObject];
[colorArray removeLastObject];
[colorArray insertObject:lastColor atIndex:0];
NSArray * shiftedColors = [NSArray arrayWithArray:colorArray];
[self.progressGradientLayer setColors:shiftedColors];
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"colors"];
[animation setToValue:shiftedColors];
[animation setDuration:0.1];
[animation setFillMode:kCAFillModeForwards];
[animation setDelegate:self];
[self.progressGradientLayer addAnimation:animation forKey:@"animateGradient"];
}
特殊效果
第一种类似于push推动的效果:
-(void)setupGradientPush {
self.pushLayer= [CAGradientLayer layer];
self.pushLayer.backgroundColor = [UIColor blueColor].CGColor;
self.pushLayer.frame =CGRectMake(240, 80, 120, 100);
//颜色分割线
self.pushLayer.locations = @[@(self.pushProgress-0.1), @(self.pushProgress-0.05), @(self.pushProgress)];
// 颜色
self.pushLayer.colors = @[(__bridge id)[UIColor yellowColor].CGColor,
(__bridge id)[UIColor purpleColor].CGColor,
(__bridge id)[UIColor redColor].CGColor];
self.pushLayer.startPoint = CGPointMake(0, 0);
self.pushLayer.endPoint = CGPointMake(1, 0);
[self.view.layer addSublayer:self.pushLayer];
self.pushTimer=[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(gradientPush) userInfo:nil repeats:YES];
self.pushTimer.fireDate=[[NSDate date] dateByAddingTimeInterval:3];
}
-(void)gradientPush {
self.pushProgress+=0.1;
if (self.pushProgress >= 1)
{
self.pushLayer.locations = @[@(-0.1), @(-0.05), @(0)];
[self.pushTimer invalidate];
return;
}
self.pushLayer.locations = @[@(self.pushProgress-0.1), @(self.pushProgress-0.05), @(self.pushProgress)];
}
第二个那个圆的渐变如果是第一次并不能立即想到是渐变色完成的效果,如果思路不是很清楚,先看下下面这张图:
默认的渐变是有颜色,其实和结束是一种颜色,中间是白色,我们通过mask将矩形的轮廓设置为空心圆,就可以看到第一张图的效果:
-(void)setupCircle {
CGFloat width=120;
CGFloat radius=width/2;
CAGradientLayer *circleLayer = [CAGradientLayer layer];
circleLayer.frame =CGRectMake(20, 200, width+4, width+4);
circleLayer.colors = @[(__bridge id)[UIColor redColor].CGColor,
(__bridge id)[UIColor whiteColor].CGColor,
(__bridge id)[UIColor redColor].CGColor];
// 颜色分割点
circleLayer.locations = @[@(-0.2), @(-0.1), @(0)];
circleLayer.startPoint = CGPointMake(0, 0);
circleLayer.endPoint = CGPointMake(1, 0);
CAShapeLayer *shapelayer = [CAShapeLayer layer];
shapelayer.frame=circleLayer.bounds;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius+2, radius+2)
radius:radius
startAngle:0
endAngle:M_PI*2
clockwise:YES];
shapelayer.path = path.CGPath;
// 设置填充颜色为透明,若设置为其他颜色将显示渐变层的颜色
shapelayer.fillColor = [UIColor clearColor].CGColor;
shapelayer.strokeColor = [UIColor redColor].CGColor;
shapelayer.strokeEnd = 1.0f;
[self.view.layer addSublayer:circleLayer];
circleLayer.mask = shapelayer;
self.circleLayer=circleLayer;
self.circleTimer=[NSTimer scheduledTimerWithTimeInterval:CircleDuration target:self selector:@selector(gradientCircle) userInfo:nil repeats:YES];
self.circleTimer.fireDate=[[NSDate date] dateByAddingTimeInterval:2];
}
-(void)gradientCircle{
CABasicAnimation *rotationAnim = [CABasicAnimation animationWithKeyPath:@"locations"];
rotationAnim.fromValue = @[@(-0.2), @(-0.1), @(0)];
rotationAnim.toValue = @[@(1.0), @(1.1), @(1.2)];
rotationAnim.duration = CircleDuration;
[self.circleLayer addAnimation:rotationAnim forKey:@"rotationAnim"];
}
最后一个图也非常有意思,这个有点躲猫猫的意思,两张图片在同一个位置,第一个图片是荷叶,第二个裸背,通过mask只显示第二张图片,加入动画,改变渐变层的位置:
-(void)setupGirl {
CGSize screenSize=[[UIScreen mainScreen] bounds].size;
CGRect imgRect=CGRectMake(0, 360,screenSize.width, screenSize.height-360);
self.bgImageView=[[UIImageView alloc]initWithFrame:imgRect];
self.bgImageView.image=[UIImage imageNamed:@"Girl"];
self.bgImageView.contentMode=UIViewContentModeScaleAspectFill;
self.bgImageView.clipsToBounds=YES;
[self.view addSubview:self.bgImageView];
self.frontImgView=[[UIImageView alloc]initWithFrame:imgRect];
self.frontImgView.image=[UIImage imageNamed:@"Girl1"];
self.frontImgView.contentMode=UIViewContentModeScaleAspectFill;
self.frontImgView.clipsToBounds=YES;
[self.view addSubview:self.frontImgView];
self.imgMaskLayer= [CAGradientLayer layer];
self.imgMaskLayer.frame =self.frontImgView.bounds;
self.imgMaskLayer.backgroundColor=[UIColor clearColor].CGColor;
self.frontImgView.layer.mask=self.imgMaskLayer;
self.imgLeftLayer=[CAGradientLayer layer];
self.imgLeftLayer.frame = CGRectMake(0, 0, screenSize.width/2, screenSize.height*3);
self.imgLeftLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor,
(__bridge id)[UIColor blackColor].CGColor];
self.imgLeftLayer.locations =@[@(0.5)];
self.imgLeftLayer.startPoint =CGPointMake(0.5, 0.3);
self.imgLeftLayer.endPoint = CGPointMake(0.5, 0.6);
[self.imgMaskLayer addSublayer:self.imgLeftLayer];
self.imgRightLayer=[CAGradientLayer layer];
self.imgRightLayer.frame = CGRectMake(screenSize.width/2, 0, screenSize.width/2, screenSize.height*3);
self.imgRightLayer.colors = @[(__bridge id)[UIColor clearColor].CGColor,
(__bridge id)[UIColor blackColor].CGColor];
self.imgRightLayer.locations =@[@(0.5)];
self.imgRightLayer.startPoint =CGPointMake(0.5, 0.3);
self.imgRightLayer.endPoint = CGPointMake(0.5, 0.6);
[self.imgMaskLayer addSublayer:self.imgRightLayer];
[self setupShimmer];
}
-(void)setupShimmer {
CGSize screenSize=[[UIScreen mainScreen] bounds].size;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(CircleDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
CABasicAnimation *basicAnimation=[CABasicAnimation animationWithKeyPath:@"position"];
basicAnimation.fromValue=[NSValue valueWithCGPoint:self.imgLeftLayer.position];
basicAnimation.toValue=[NSValue valueWithCGPoint:CGPointMake(self.imgLeftLayer.position.x,-screenSize.height)];
basicAnimation.duration=1.5f;
basicAnimation.removedOnCompletion=false;
basicAnimation.fillMode=kCAFillModeForwards;
[self.imgLeftLayer addAnimation:basicAnimation forKey:@"LeftPosition"];
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((CircleDuration+1.5)* NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
CABasicAnimation *basicAnimation=[CABasicAnimation animationWithKeyPath:@"position"];
basicAnimation.fromValue=[NSValue valueWithCGPoint:self.imgRightLayer.position];
basicAnimation.toValue=[NSValue valueWithCGPoint:CGPointMake(self.imgRightLayer.position.x,-screenSize.height/2)];
basicAnimation.duration=1.5;
basicAnimation.removedOnCompletion=false;
basicAnimation.fillMode=kCAFillModeForwards;
[self.imgRightLayer addAnimation:basicAnimation forKey:@"RightPosition"];
});
}
基本上常见的CAGradientLayer已经实现,如果有其他效果欢迎探讨
GitHub地址:FELayerAnimation-FlyElephant