CAGradientLayer渐变颜色动画

简介:

CAGradientLayer渐变颜色动画

或许你用过CAGradientLayer,你知道他是用于渐变颜色的,但你是否直到,CAGradientLayer的渐变颜色是可以动画的哦.

源码:

//
//  RootViewController.m
//  CAGradientLayer
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "CAShapeLayer+Circle.h"
#import "YXGCD.h"

@interface RootViewController ()

@property (nonatomic, strong) GCDTimer  *timer;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor blackColor];
    
    // 创建形状遮罩
    CAShapeLayer *circleLayer = [CAShapeLayer LayerWithCircleCenter:CGPointMake(82, 82)
                                                             radius:80
                                                         startAngle:DEGREES(0)
                                                           endAngle:DEGREES(360)
                                                          clockwise:YES
                                                    lineDashPattern:@[@5, @5]];
    circleLayer.strokeColor   = [UIColor blackColor].CGColor;   // 边缘线的颜色
    circleLayer.lineCap       = kCALineCapSquare;               // 边缘线的类型
    circleLayer.lineWidth     = 1.0f;                           // 线条宽度
    circleLayer.strokeStart   = 0.0f;
    circleLayer.strokeEnd     = 1.0f;
    
    // 创建渐变图层
    CAGradientLayer *faucet = [CAGradientLayer layer];
    faucet.frame            = CGRectMake(0, 0, 200, 200);
    faucet.position         = self.view.center;
    
    // 以CAShapeLayer的形状作为遮罩是实现特定颜色渐变的关键
    faucet.mask   = circleLayer;
    faucet.colors = @[(id)[UIColor greenColor].CGColor,
                      (id)[UIColor redColor].CGColor,
                      (id)[UIColor cyanColor].CGColor];
    
    // 设定动画时间
    faucet.speed = 0.5f;

    // 添加到系统图层中
    [self.view.layer addSublayer:faucet];

    // 定时器动画事件
    _timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
    [_timer event:^{
        circleLayer.strokeEnd = arc4random() % 100 / 100.f;
        faucet.colors = @[(id)[UIColor colorWithRed:arc4random()%255/255.f
                                              green:arc4random()%255/255.f
                                               blue:arc4random()%255/255.f
                                              alpha:1].CGColor,
                          (id)[UIColor colorWithRed:arc4random()%255/255.f
                                              green:arc4random()%255/255.f
                                               blue:arc4random()%255/255.f
                                              alpha:1].CGColor,
                          (id)[UIColor colorWithRed:arc4random()%255/255.f
                                              green:arc4random()%255/255.f
                                               blue:arc4random()%255/255.f
                                              alpha:1].CGColor,
                          (id)[UIColor colorWithRed:arc4random()%255/255.f
                                              green:arc4random()%255/255.f
                                               blue:arc4random()%255/255.f
                                              alpha:1].CGColor,
                          (id)[UIColor colorWithRed:arc4random()%255/255.f
                                              green:arc4random()%255/255.f
                                               blue:arc4random()%255/255.f
                                              alpha:1].CGColor];
    } timeInterval:NSEC_PER_SEC];
    [_timer start];
}

@end

效果如下:

以下代码才是核心的地方:

 

附录:

http://stackoverflow.com/questions/21121670/cagradientlayer-with-cashapelayer-mask-not-showing

目录
相关文章
|
11天前
Echarts线性渐变、径向渐变、纹理填充
Echarts线性渐变、径向渐变、纹理填充
|
4月前
边框虚线滚动动画特效
边框虚线滚动动画特效
|
9月前
|
XML Java API
一个超简单的渐变平行四边形进度条
这个自定义View,其实就是一个简单的进度条,无非就是平行四边形形状的,加了渐变以及状态颜色的切换。
|
10月前
R—点图(渐变)
R—点图(渐变)
|
C# 图形学
C#之深入理解GDI+绘制圆弧及圆角矩形等比缩放的绘制
GDI+中对于圆弧的绘制,是以给定的长方形(Rectangle`结构)为边界绘制的椭圆的一部分形成的圆弧。绘制的圆弧的中心为长方形内切椭圆的圆心(如果是正方形,则正方形的...
419 0
C#之深入理解GDI+绘制圆弧及圆角矩形等比缩放的绘制
|
Web App开发 移动开发 前端开发
H5:画布Canvas基础知识讲解(三)之文字、阴影、颜色渐变
​上一节介绍了H5:画布Canvas基础知识讲解(二)之插入图像、像素级操作,接下来继续讲解H5:画布Canvas基础。
|
前端开发 容器
巧妙实现带圆角的渐变边框
巧妙实现带圆角的渐变边框
308 0
巧妙实现带圆角的渐变边框
SwiftUI—使用径向渐变制作从原点向外扩散的渐变颜色
SwiftUI—使用径向渐变制作从原点向外扩散的渐变颜色
384 0
SwiftUI—使用径向渐变制作从原点向外扩散的渐变颜色
SwiftUI—使用角度渐变制作顺时针的扇形渐变颜色
SwiftUI—使用角度渐变制作顺时针的扇形渐变颜色
311 0
SwiftUI—使用角度渐变制作顺时针的扇形渐变颜色
SwiftUI—使用渐变提高视觉的冲击力:绘制线性渐变
SwiftUI—使用渐变提高视觉的冲击力:绘制线性渐变
198 0
SwiftUI—使用渐变提高视觉的冲击力:绘制线性渐变