iOS-CALayer && CAAnimation
一、CALayer
1.CALayer
CALayer属于QuartzCore.framework框架,从Xcode5起我们不必要手动导入这个库。
CALayer我们可以简单理解为一个层。当我们绘制的UIView能在屏幕显示,其实质是因为这个层。
我们下面通过代码理解一下CALayer的基本用法。
CALayer *caLayer = [CALayer layer];
caLayer.backgroundColor = [UIColor cyanColor].CGColor;
caLayer.frame = CGRectMake(10, 20, 100, 100);
caLayer.cornerRadius = 20;
caLayer.masksToBounds = YES;
[self.view.layer addSublayer:caLayer];
当我们执行上述代码的时候,会在view上添加一个layer层。其效果如下图所示。

其中cornerRadius值的大小决定layer的形状,在四个角用它的长度做半切圆。我们也可以设置边框,边框颜色等信息。
CALayer *caLayer1 = [CALayer layer];
caLayer1.frame = CGRectMake(10, 20, 200, 200);
caLayer1.contents = (id)[UIImage imageNamed:@"health.jpg"].CGImage;
caLayer1.cornerRadius = 100;
caLayer1.masksToBounds = YES;
caLayer1.borderWidth = 10;
caLayer1.borderColor = [UIColor greenColor].CGColor;
[self.view.layer addSublayer:caLayer1];
效果如下:
CALayer还有一个重要的属性position(锚点默认0.5,0.5),和caLayer.anchorPoint(0-1)
我们可以通过下图理解:

执行下面代码:
CALayer *caLayer = [CALayer layer];
caLayer.backgroundColor = [UIColor cyanColor].CGColor;
caLayer.cornerRadius = ;
caLayer.bounds = CGRectMake(, , , );
caLayer.position = CGPointMake(, );
caLayer.anchorPoint = CGPointMake(0.5, );
caLayer.masksToBounds = YES;
[self.view.layer addSublayer:caLayer];
效果为:

2.CATextLayer
CATextLayer是CALayer的子类。我们可以在上面写文字,设置字体等信息。
CATextLayer *caTextlayer = [CATextLayer layer];
caTextlayer.frame = CGRectMake(10, 20, 300, 100);
caTextlayer.string = @"Roy says hello";
caTextlayer.foregroundColor = [UIColor orangeColor].CGColor;
[self.view.layer addSublayer:caTextlayer];

3.CAGradientLayer
这个类也是继承CALayer.可以实现颜色渐变。
CAGradientLayer *dLayer = [CAGradientLayer layer];
dLayer.colors = @[(id)[UIColor yellowColor].CGColor,(id)[UIColor grayColor].CGColor,(id)[UIColor redColor].CGColor,(id)[UIColor greenColor].CGColor];
dLayer.startPoint = CGPointMake(0, 0);
dLayer.endPoint = CGPointMake(1, 1);
dLayer.locations = @[@0.0,@0.2,@0.5,@01];//0-1
dLayer.frame = CGRectMake(10, 20, 320, 100);
[self.view.layer addSublayer:dLayer];

二、CAAnimation
关于CAAnimation,能够看懂下面一幅图和下面的代码即可。
#import "ViewController.h" @interface ViewController ()
{
CALayer *layer;
}
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
layer = [CALayer layer];
layer.frame = CGRectMake(, , , );
layer.backgroundColor = [UIColor grayColor].CGColor;
[self.view.layer addSublayer:layer]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(, , , );
button.backgroundColor = [UIColor purpleColor];
[button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// [self basicAnimation];
// [self keyframeAnimation];
// [self groupAnimation]; }
-(void)btnClick
{
//过渡动画,只有在点击事件中才能执行
[self transitionAnimation];
}
//基础动画,继承属性动画
-(void)basicAnimation
{
/*
//背景颜色变换动画
CABasicAnimation *animation = [CABasicAnimation animation];
//The key-path describing the property to be animated
animation.keyPath = @"backgroundColor";
//动画周期
animation.duration = 2;
//从哪个属性开始动画
animation.fromValue = (id)[UIColor grayColor].CGColor;
//到哪个属性结束动画
animation.toValue = (id)[UIColor greenColor].CGColor;
[layer addAnimation:animation forKey:nil];
*/ //位置移动 CABasicAnimation *animation = [CABasicAnimation animation];
animation.keyPath = @"position";
animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(, )];
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(, )];
animation.duration = ;
[layer addAnimation:animation forKey:nil];
} //帧动画,继承属性动画
-(void)keyframeAnimation
{
/*
CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
//动画的属性
animation.keyPath = @"backgroundColor";
//动画过渡值
animation.values = @[(id)[UIColor redColor].CGColor,(id)[UIColor greenColor].CGColor,(id)[UIColor purpleColor].CGColor];
//动画过渡时间
animation.keyTimes = @[@0.0,@0.5,@1];
animation.duration = 2;
[layer addAnimation:animation forKey:nil];
*/ CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
animation.keyPath = @"position";
animation.values = @[[NSValue valueWithCGPoint:CGPointMake(, )],[NSValue valueWithCGPoint:CGPointMake(, )],[NSValue valueWithCGPoint:CGPointMake(, )],[NSValue valueWithCGPoint:CGPointMake(, )],[NSValue valueWithCGPoint:CGPointMake(, )],[NSValue valueWithCGPoint:CGPointMake(, )]];
animation.autoreverses = YES;
animation.duration = ;
[layer addAnimation:animation forKey:nil];
} //组动画,组合动画,多个动画同时执行
-(void)groupAnimation
{
//移动
CABasicAnimation *basic =[CABasicAnimation animation ];
basic.keyPath = @"position";
basic.duration = ;
basic.autoreverses = YES;
basic.fromValue = [NSValue valueWithCGPoint:layer.position];
basic.byValue = [NSValue valueWithCGPoint:CGPointMake(, )];
//颜色变化
CAKeyframeAnimation *keyframe = [CAKeyframeAnimation animation];
keyframe.keyPath = @"backgroundColor";
keyframe.values = @[(id)[UIColor redColor].CGColor,(id)[UIColor yellowColor].CGColor,(id)[UIColor greenColor].CGColor];
keyframe.duration = ;
keyframe.autoreverses = YES; CAAnimationGroup *group = [CAAnimationGroup animation];
group.animations = @[basic,keyframe];
//这边时间是以group的时间为主的
group.duration = ;
[layer addAnimation:group forKey:nil];
} //过渡动画
- (void)transitionAnimation
{
CATransition *animation = [CATransition animation];
animation.type = @"pageUnCurl";
animation.delegate = self;
animation.duration = ;
animation.autoreverses = YES;
[layer addAnimation:animation forKey:nil]; }
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
Animation
其中过度动画的类型,我们可以使用下面的效果。
cube 方块
suckEffect 三角
rippleEffect 水波抖动
pageCurl 上翻页
pageUnCurl 下翻页
cameraIrisHollowOpen 镜头快门开
cameraIrisHollowClose 镜头快门开
iOS-CALayer && CAAnimation的更多相关文章
- iOS开发CAAnimation详解
Core Animation,即为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就可以实现非常强大的功能.Core Anima ...
- IOS CALayer的属性和使用
一.CALayer的常用属性 1.@propertyCGPoint position; 图层中心点的位置,类似与UIView的center:用来设置CALayer在父层中的位置:以父层的左上角为原点( ...
- iOS CALayer应用详解
跟着大神一起进步,本篇博客原文地址:http://blog.csdn.net/hello_hwc?viewmode=contents 一 CALayer是什么? Layers是绘图和动画的基础, L ...
- IOS CALayer(二)
UIview内部有个默认的CALayer对象层,虽然我门不可以重新创建它,但是我门可以再其上面添加子层. 我们知道,UIView有 addSubview:方法,同样,CALayer也有addSubla ...
- IOS CALayer(一)
对于一个app的好坏,我们首要判断的便是app的界面,而界面的建立则是在图形的处理基础上的,说到图形处理又不得不提及Quartz2D,CALayer. 在iOS系统中,你能看得见摸得着的东西基本上都是 ...
- iOS CALayer动画中使用的3个tree
在网上经常看到关于layer的tree的描述,不太理解,今天找到了官方文档,原文在Core Animation Programming Guide 中. Layer Trees Reflect Dif ...
- IOS CALayer是什么
大家在开发IOS程序时,经常会遇到self.view.layer这个东西,我以前也是不求甚解,后来觉得有必要整理下. 简单介绍layer: 在IOS中,你能看得见摸得着的东西都是UIView,比如一个 ...
- iOS - CALayer 绘图层
1.CALayer 绘图层 在 iOS 系统中,你能看得见摸得着的东西基本上都是 UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是 UIView.其实 UIView 之 ...
- iOS CALayer使用
CALayer使用 iOS的设备中,我们之所以能看到各种各样的控件.文字.图片,都是Core Animation框架的功劳.它通过图层的合成,最终显示在屏幕上.而今天这篇文章讲的就是Core Anim ...
- iOS CALayer之CAEmitterLayer粒子发射器的神奇效果
https://www.jianshu.com/p/c54ffd7412e7 想必以前QQ空间的点赞效果大家都知道吧,点赞之后按钮周围会有一圈爆裂的小圆点:还有微信的红包雨表情动画等,以及烟花,火焰效 ...
随机推荐
- BFS 简单思想以及代码
BFS(广搜思想) 广度优先搜索 广度优先搜索是图论的搜索算法之一,以下便进行简单叙述 对于每一个顶点来说,都存在着三种颜色 白色,灰色,黑色 而对于每个顶点,都有三种数据类型 颜色类型,前驱或者父节 ...
- 报bug
在打印输出seg的gt数据的时候,出现了gt数据突然很大突然很小的情况,一般这种都是访问了其他内存
- iperf安装与使用
从官网下载相应版本. https://iperf.fr/iperf-download.php centos7 安装 rpm -i iperf3-3.1.3-1.fc24.x86_64.rpm ubun ...
- 《队长说得队》第八次团队作业Alpha冲刺
项目 内容 这个作业属于哪个课程 >>2016级计算机科学与工程学院软件工程(西北师范大学) 这个作业的要求在哪里 >>实验十二 团队作业8:软件测试与ALPHA冲刺 团队名称 ...
- 阿里云服务器下安装LAMP环境(CentOS Linux 6.3)
http://ninghao.net/course/514 http://www.sphinxsearch.org/archives/243 ctrl + l putty 清理屏幕 apache 自 ...
- iOSAES加密的实现
+(NSData *)AES256ParmEncryptWithKey:(NSString *)key Encrypttext:(NSData *)text //加密 { char keyPtr[k ...
- DNA Pairing-freecodecamp算法题目
DNA Pairing 1.要求 DNA 链缺少配对的碱基.依据每一个碱基,为其找到配对的碱基,然后将结果作为第二个数组返回. Base pairs(碱基对)是一对 AT 和 CG,为给定的字母匹配缺 ...
- NOIP模拟赛 双色球
[题目描述] 机房来了新一届的学弟学妹,邪恶的chenzeyu97发现一位学弟与他同名,于是他当起了善良的学长233 “来来来,学弟,我考你道水题检验一下你的水平……” 一个栈内初始有n个红色和蓝色的 ...
- 机器学习(一)之KNN算法
knn算法原理 ①.计算机将计算所有的点和该点的距离 ②.选出最近的k个点 ③.比较在选择的几个点中那个类的个数多就将该点分到那个类中 KNN算法的特点: knn算法的优点:精度高,对异常值不敏感,无 ...
- C#基础-字符串
字符串比较,strA.CompareTo(strB) A大于B 正数 A小于B 负数 A等于B 0 string strA = "ab"; string strB = " ...