一、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的更多相关文章

  1. iOS开发CAAnimation详解

    Core Animation,即为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就可以实现非常强大的功能.Core Anima ...

  2. IOS CALayer的属性和使用

    一.CALayer的常用属性 1.@propertyCGPoint position; 图层中心点的位置,类似与UIView的center:用来设置CALayer在父层中的位置:以父层的左上角为原点( ...

  3. iOS CALayer应用详解

    跟着大神一起进步,本篇博客原文地址:http://blog.csdn.net/hello_hwc?viewmode=contents 一 CALayer是什么? Layers是绘图和动画的基础,  L ...

  4. IOS CALayer(二)

    UIview内部有个默认的CALayer对象层,虽然我门不可以重新创建它,但是我门可以再其上面添加子层. 我们知道,UIView有 addSubview:方法,同样,CALayer也有addSubla ...

  5. IOS CALayer(一)

    对于一个app的好坏,我们首要判断的便是app的界面,而界面的建立则是在图形的处理基础上的,说到图形处理又不得不提及Quartz2D,CALayer. 在iOS系统中,你能看得见摸得着的东西基本上都是 ...

  6. iOS CALayer动画中使用的3个tree

    在网上经常看到关于layer的tree的描述,不太理解,今天找到了官方文档,原文在Core Animation Programming Guide 中. Layer Trees Reflect Dif ...

  7. IOS CALayer是什么

    大家在开发IOS程序时,经常会遇到self.view.layer这个东西,我以前也是不求甚解,后来觉得有必要整理下. 简单介绍layer: 在IOS中,你能看得见摸得着的东西都是UIView,比如一个 ...

  8. iOS - CALayer 绘图层

    1.CALayer 绘图层 在 iOS 系统中,你能看得见摸得着的东西基本上都是 UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是 UIView.其实 UIView 之 ...

  9. iOS CALayer使用

    CALayer使用 iOS的设备中,我们之所以能看到各种各样的控件.文字.图片,都是Core Animation框架的功劳.它通过图层的合成,最终显示在屏幕上.而今天这篇文章讲的就是Core Anim ...

  10. iOS CALayer之CAEmitterLayer粒子发射器的神奇效果

    https://www.jianshu.com/p/c54ffd7412e7 想必以前QQ空间的点赞效果大家都知道吧,点赞之后按钮周围会有一圈爆裂的小圆点:还有微信的红包雨表情动画等,以及烟花,火焰效 ...

随机推荐

  1. 阻止form元素内的input标签回车提交表单

    <form></form>标签内input元素回车会默认提交表单. 阻止回车默认提交表单: $('form').on('keydown', function (event) { ...

  2. 总结一下自己脑海里的JavaScript吧(一)--DOM模型

    今天是2019年6月25日,闲来无事,写一篇文章来看看自己脑袋里装了多少JavaScript知识! 这儿就第一章: 说起JavaScript,它是什么?后端脚本语言?前端编程语言?还是在网站浏览器上运 ...

  3. apropos linux

    Apropos adj. 恰当的,关于,就...而言 adv. 顺便地,恰当地 All my suggestions apropos the script were accepted. 我所有有关该剧 ...

  4. 如何远程连接Windows server上的MySQL服务

    废话不多说,直接开干 首先要打开服务器的MySQL端口号:3306(当然,也可以把服务器的防火墙直接关闭,不过不安全) 1.打开服务器管理器,有个高级安全Windows防火墙,下面有一个入站规则, 右 ...

  5. C Library - <limits.h>

    Introduction The limits.h header determines various properties of the various variable types. The ma ...

  6. NSRegularExpression 使用

    需求: // 后台返回的某个实体 reminder = { cost = , type = , template = 可免费做某事{time}分钟,超过将按{cost}K元收费, time = } t ...

  7. C++函数的默认参数补充

    1.函数定义时指定默认参数 在C++中,定义函数时可以给形参指定一个默认的值,这样调用函数时如果没有给这个形参赋值(没有对应的实参),那么就使用这个默认的值.也就是说,调用函数时可以省略有默认值的参数 ...

  8. centos 7 安装WordPress的参考博文

    安装方法: https://www.cnblogs.com/flankershen/p/7476415.html 安装完,测试不成功的解决办法: https://blog.csdn.net/u0104 ...

  9. python入门学习笔记1:Python与C的简单区别

    转载于:https://www.cnblogs.com/mlgjb/p/7892130.html 并做适当修改 一:简单比较   C语言 python 执行速度 快 慢 跨平台 不可以 可以 用途 操 ...

  10. CodeForce:732B-Cormen — The Best Friend Of a Man

    传送门:http://codeforces.com/problemset/problem/732/B Cormen - The Best Friend Of a Man time limit per ...