CAGradientLayer功能
一、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功能的更多相关文章
- iOS之小功能模块--彩虹动画进度条学习和自主封装改进
前言: 首先展示一下这个iOS小示例的彩色进度条动画效果: 阅读本文先说说好处:对于基础不好的读者,可以直接阅读文末尾的"如何使用彩虹动画进度条"章节,然后将我封装好的这个功能模块 ...
- iOS 之使用CAShapeLayer中的CAGradientLayer实现圆环的颜色渐变
本文转载自:http://blog.csdn.net/zhoutao198712/article/details/20864143 在 Github上看到一些进度条的功能,都是通过Core Graph ...
- Reader开发(二)增加PDF阅读功能
最近任务很多很忙,所以更新博客的速度很慢. 大概上周就为Reader加了一个PDF阅读的功能,但是一直没时间写上来.昨晚找一下文件发现扩展了功能的Demo居然在文件目录下看不到任何文件,但是却显示有文 ...
- 将CAGradientLayer用作maskView的遮罩图层
将CAGradientLayer用作maskView的遮罩图层 说明 CAGradientLayer可以用作alpha遮罩,供maskView使用,这两种东西都是非常偏门的东西,但是,偏门不代表功能不 ...
- 制作类似ThinkPHP框架中的PATHINFO模式功能
一.PATHINFO功能简述 搞PHP的都知道ThinkPHP是一个免费开源的轻量级PHP框架,虽说轻量但它的功能却很强大.这也是我接触学习的第一个框架.TP框架中的URL默认模式即是PathInfo ...
- PHP搭建大文件切割分块上传功能
背景 在网站开发中,文件上传是很常见的一个功能.相信很多人都会遇到这种情况,想传一个文件上去,然后网页提示"该文件过大".因为一般情况下,我们都需要对上传的文件大小做限制,防止出现 ...
- SQL Server2014 SP2新增的数据库克隆功能
SQL Server2014 SP2新增的数据库克隆功能 创建测试库 --创建测试数据库 create database testtest use testtest go --创建表 )) --插入数 ...
- SQL Server 数据加密功能解析
SQL Server 数据加密功能解析 转载自: 腾云阁 https://www.qcloud.com/community/article/194 数据加密是数据库被破解.物理介质被盗.备份被窃取的最 ...
- Taurus.MVC 2.2 开源发布:WebAPI 功能增强(请求跨域及Json转换)
背景: 1:有用户反馈了关于跨域请求的问题. 2:有用户反馈了参数获取的问题. 3:JsonHelper的增强. 在综合上面的条件下,有了2.2版本的更新,也因此写了此文. 开源地址: https:/ ...
随机推荐
- JAVA 学习笔记 - 基础语法1
1. 类的定义 public class 与 class的区别 一个java文件只有一个public class, 而且类名必须与文件名一致. 一个java文件可以有多个class定义,javac ...
- code blocks 安装与实践
背景 因为不是主要修习C/C++,仅用于写算法和数据结构,code blocks轻量但是安装老是出现问题,故有此记录 安装 官方地址:http://www.codeblocks.org/downloa ...
- Codeforces 280C - Game on Tree
传送门:280C - Game on Tree 不知道期望是啥的请自行Baidu或Google,(溜了 题目大意,有一棵有根树,每次随机选择一个节点,将这个节点和它的子树删除,问将整棵树删除的期望次数 ...
- The content of element type "resultMap" must match ...
mybatis中的mapper文件错误 ①错误原因: <resultMap>标签中需要按照一下顺序编写: <id> <result> <association ...
- vue-router2.0二级路由的简单使用
1.app.vue中 <template> <div id="app"> <router-view></router-view> & ...
- graph.h
#ifndef _GRAPH_#define _GRAPH_#include<stdio.h>#include<stdlib.h>#include<string.h> ...
- 以位为单位存储标志-共用体-union
一.程序的结构如下: typedef union _KEYST { struct { uint8 Key1_Flag :1;//表示第0 ...
- 完美解决在Servlet中出现一个输出中文乱码的问题
@Override public void doPost(HttpServletRequest reqeust, HttpServletResponse response) throws Servle ...
- 设置idea快速生成doc comment
在eclipse中快速生成方法的doc comment快捷键是Alt+Shift+J, 而在idea中没有对应的快捷键,但是能实现相似的功能. 1.在方法上面输入/** 然后回车就可以生成注释了. 2 ...
- 设置mysql5.7远程连接-----------https://blog.csdn.net/qiyueqinglian/article/details/52778230
https://blog.csdn.net/qiyueqinglian/article/details/52778230 设置mysql5.7远程连接