一、CAGradientLayer介绍

、CAGradientLayer是用于处理渐变色的层结构

、CAGradientLayer的渐变色能够做隐式动画

、大部分情况下。CAGradientLayer都是与CAShapeLayer配合使用的

、CAGradientLayer能够用作png遮罩效果

二、CAGradientLayer坐标系统

1、CAGradientLayer的坐标系统是从坐标(0,0)到(1,1)绘制的矩形

、CAGradientLayer的frame值的size不为正方形的话,坐标系统会被拉伸

、CAGradientLayer的startPoint与endPoint会直接影响颜色的绘制方向

、CAGradientLayer的颜色切割点是以到的比例来计算的

三、色差动画的实现

1、确定渐变色渐变方向

2、设定两种颜色,当中一种是透明色,第二种是自己定义颜色

3、设定好location颜色切割点值

、CAGradientLayer的颜色切割点是以到的比例来计算的

@property (nonatomic,strong) CAGradientLayer *gradientLayer;//渐变层

@property (nonatomic,strong) NSTimer         *timer;       //
定时器

- (void) colors {

self.view.backgroundColor = [UIColorblackColor];

//
创建背景图片

UIImageView *imageView = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"bg"]];

imageView.center       =self.view.center;

[self.viewaddSubview:imageView];

//
初始化渐变层

self.gradientLayer       = [CAGradientLayerlayer];

self.gradientLayer.frame = imageView.bounds;

[imageView.layeraddSublayer:self.gradientLayer];

//设定颜色渐变方向

,);

,);

//
设定颜色组

self.gradientLayer.colors =@[(__bridgeid)[UIColorclearColor].CGColor,

(__bridgeid)[UIColorredColor].CGColor];

//
设定颜色切割点

self.gradientLayer.locations =
@[@(0.5f), @(1.f)];

//
初始化定时器

self.timer = [NSTimerscheduledTimerWithTimeInterval:2.f

target:self

selector:@selector(timerEvent)

userInfo:nil

repeats:YES];

}

- (void)timerEvent {

//
设定颜色组动画

self.gradientLayer.colors =@[(__bridgeid)[UIColorclearColor].CGColor,

(__bridgeid)[UIColorcolorWithRed:arc4random() % /255.f

green:arc4random() % /255.f

blue:arc4random() % /255.f

alpha:].CGColor];

//设定颜色切割点动画

/10.f),@(1.f)];

}

四、用CAGradientLayer实现带色差动画的View

1、确定几个属性值

2、确定能够做动画的參数

3、重写setter方法做动画

typedefenum : NSUInteger {

UP,   // 从上往下

DOWN, // 从下往上

RIGHT,// 从右往左

LEFT, // 从左往右

} EColorDirection;

@interface ColorUIImageView :UIImageView

/**

*  确定方向(能够做动画)

*/

@property (nonatomic,assign) EColorDirection  direction;

/**

*  颜色(能够做动画)

*/

@property (nonatomic,strong)UIColor  *color;

/**

*  百分比(能够做动画)

*/

@property (nonatomic,assign)CGFloat   percent;

@end

#import "ColorUIImageView.h"

@interface
ColorUIImageView ()

@property (nonatomic,strong)CAGradientLayer *gradientLayer;

@end

@implementation ColorUIImageView

- (instancetype)initWithFrame:(CGRect)frame

{

self = [superinitWithFrame:frame];

if (self) {

//
初始化CAGradientLayer

self.gradientLayer           = [CAGradientLayerlayer];

self.gradientLayer.frame     =self.bounds;

self.gradientLayer.colors    =@[(__bridgeid)[UIColorclearColor].CGColor,

(__bridgeid)[UIColorredColor].CGColor];

self.gradientLayer.locations =@[@(1),@(1)];

[self.layeraddSublayer:self.gradientLayer];

}

return
self;

}

#pragma mark - 重写setter,getter方法

@synthesize color = _color;

- (void)setColor:(UIColor *)color {

_color = color;

self.gradientLayer.colors =@[(__bridgeid)[UIColorclearColor].CGColor,

(__bridgeid)color.CGColor];

}

- (UIColor *)color {

return _color;

}

@synthesize percent =_percent;

- (void)setPercent:(CGFloat)percent {

_percent = percent;

self.gradientLayer.locations =@[@(percent),@(1)];

}

- (CGFloat)percent {

return_percent;

}

@synthesize direction =_direction;

- (void)setDirection:(EColorDirection)direction {

_direction = direction;

if (direction == UP) {

,);

,);

}else if (direction ==DOWN) {

,);

,);

}else if (direction ==RIGHT) {

,);

,);

}else if (direction ==LEFT) {

,);

,);

}else {

,);

,);

}

}

- (EColorDirection)direction {

return_direction;

}

@end

#import "ColorUIImageView.h"

@interface ViewController ()

@property (nonatomic,strong)ColorUIImageView *colorView;

@end

@implementation ViewController

- (void)viewDidLoad {

[superviewDidLoad];

,,,
)];

self.colorView.center =self.view.center;

self.colorView.image  = [UIImageimageNamed:@"bg"];

[self.viewaddSubview:self.colorView];

[selfperformSelector:@selector(event)

withObject:nil

afterDelay:1.f];

}

- (void)event {

self.colorView.direction =DOWN;

self.colorView.color     = [UIColorcyanColor];

self.colorView.percent   =0.5;

}

@end

CAGradientLayer功能的更多相关文章

  1. iOS之小功能模块--彩虹动画进度条学习和自主封装改进

    前言: 首先展示一下这个iOS小示例的彩色进度条动画效果: 阅读本文先说说好处:对于基础不好的读者,可以直接阅读文末尾的"如何使用彩虹动画进度条"章节,然后将我封装好的这个功能模块 ...

  2. iOS 之使用CAShapeLayer中的CAGradientLayer实现圆环的颜色渐变

    本文转载自:http://blog.csdn.net/zhoutao198712/article/details/20864143 在 Github上看到一些进度条的功能,都是通过Core Graph ...

  3. Reader开发(二)增加PDF阅读功能

    最近任务很多很忙,所以更新博客的速度很慢. 大概上周就为Reader加了一个PDF阅读的功能,但是一直没时间写上来.昨晚找一下文件发现扩展了功能的Demo居然在文件目录下看不到任何文件,但是却显示有文 ...

  4. 将CAGradientLayer用作maskView的遮罩图层

    将CAGradientLayer用作maskView的遮罩图层 说明 CAGradientLayer可以用作alpha遮罩,供maskView使用,这两种东西都是非常偏门的东西,但是,偏门不代表功能不 ...

  5. 制作类似ThinkPHP框架中的PATHINFO模式功能

    一.PATHINFO功能简述 搞PHP的都知道ThinkPHP是一个免费开源的轻量级PHP框架,虽说轻量但它的功能却很强大.这也是我接触学习的第一个框架.TP框架中的URL默认模式即是PathInfo ...

  6. PHP搭建大文件切割分块上传功能

    背景 在网站开发中,文件上传是很常见的一个功能.相信很多人都会遇到这种情况,想传一个文件上去,然后网页提示"该文件过大".因为一般情况下,我们都需要对上传的文件大小做限制,防止出现 ...

  7. SQL Server2014 SP2新增的数据库克隆功能

    SQL Server2014 SP2新增的数据库克隆功能 创建测试库 --创建测试数据库 create database testtest use testtest go --创建表 )) --插入数 ...

  8. SQL Server 数据加密功能解析

    SQL Server 数据加密功能解析 转载自: 腾云阁 https://www.qcloud.com/community/article/194 数据加密是数据库被破解.物理介质被盗.备份被窃取的最 ...

  9. Taurus.MVC 2.2 开源发布:WebAPI 功能增强(请求跨域及Json转换)

    背景: 1:有用户反馈了关于跨域请求的问题. 2:有用户反馈了参数获取的问题. 3:JsonHelper的增强. 在综合上面的条件下,有了2.2版本的更新,也因此写了此文. 开源地址: https:/ ...

随机推荐

  1. 【Python-2.7】删除空格

    有时我们在编程过程中,需要去除字符串两边的空格,可以用如下函数解决问题: rstrip():去除字符串右边的空格: lstrip():去除字符串左边的空格: strip():去除字符串两边的空格. 示 ...

  2. swing jTable排序问题(点击表头排序)

    1.JDK6自带排序实现: tableName.setAutoCreateRowSorter(true); 2.其实界面设计中勾选一个属性就搞定了: .

  3. 获取WebBrowser全cookie 和 httpWebRequest 异步获取页面数据

    获取WebBrowser全cookie [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true) ...

  4. BackboneJS and UnderscoreJS

     介绍 来自API(backbone能做什么?) 当我们开发含有大量Javascript的web应用程序时,首先你需要做的事情之一便是停止向DOM对象附加数据. 通过复杂多变的jQuery选择符和回调 ...

  5. 动态设置缩放比例和html字体大小

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8& ...

  6. 在eclipse里如何快速定位到某一行?

    使用快捷键ctrl+L讲每一行的行号显示出来:在eclipse的某一行的最左边,右键——show Line Numbers就可以将行数都显示出来.

  7. wparam , lparam 传递消息

    01.WM_PAINT消息 LOWORD(lParam)是客户区的宽,HIWORD(lParam)是客户区的高 02.滚动条WM_VSCROLL或WM_HSCROLL消息 LOWORD(wParam) ...

  8. CLISTCTRL2

    回顾: 刚刚写完,因为是分期写的,所以最初想好好做一下的文章格式半途而废了~说的也许会有点啰嗦,但是所有的基础用到的技术细节应该都用到了. 如果还有什么疑问,请回复留言,我会尽力解答. 如果有错误,请 ...

  9. Java变量及数据类型

    变量及数据类型 变量 变量定义格式:数据类型 变量名 = 初始化值; 基本数据类型 整形数据 package com.ahabest.demo; //输出整形数据的最小值,默认值,最大值,二进制位数 ...

  10. 一个小demo熟悉Spring Boot 和 thymeleaf 的基本使用

    目录 介绍 零.项目素材 一. 创建 Spring Boot 项目 二.定制首页 1.修改 pom.xml 2.引入相应的本地 css.js 文件 3.编辑 login.html 4.处理对 logi ...