iOS开发 - CALayer图层
CALayer的基本使用
在iOS中。你能看得见摸得着的东西基本上都是UIView。比方一个button、一个文本标签、一个文本输入框、一个图标等等。这些都是UIView
事实上UIView之所以能显示在屏幕上,全然是由于它内部的一个图层
在创建UIView对象时,UIView内部会自己主动创建一个图层(即CALayer对象)。通过UIView的layer属性能够訪问这个层
@property(nonatomic,readonly,retain) CALayer *layer;
当UIView须要显示到屏幕上时,会调用drawRect:方法进行画图,而且会将全部内容绘制在自己的图层上,画图完毕后,系统会将图层复制到屏幕上,于是就完毕了UIView的显示
换句话说,UIView本身不具备显示的功能,是它内部的层才有显示功能
通过操作CALayer对象,能够非常方便地调整UIView的一些外观属性,比方:
阴影
圆角大小
边框宽度和颜色
… …
还能够给图层加入动画,来实现一些比較炫酷的效果
CALayer的属性
//宽度和高度
@property CGRect bounds;
//位置(默认指中点。详细由anchorPoint决定)
@property CGPoint position;
//锚点(x,y的范围都是0-1)。决定了position的含义
@property CGPoint anchorPoint;
//背景颜色(CGColorRef类型)
@property CGColorRef backgroundColor;
//形变属性
@property CATransform3D transform;
//边框颜色(CGColorRef类型)
@property CGColorRef borderColor;
//边框宽度
@property CGFloat borderWidth;
//圆角半径
@property CGFloat cornerRadius;
//内容(比方设置为图片CGImageRef)
@property(retain) id contents;
关于CALayer的疑惑
首先
CALayer是定义在QuartzCore框架中的(Core Animation)
CGImageRef、CGColorRef两种数据类型是定义在CoreGraphics框架中的
UIColor、UIImage是定义在UIKit框架中的
其次
QuartzCore框架和CoreGraphics框架是能够跨平台使用的,在iOS和Mac OS X上都能使用
可是UIKit仅仅能在iOS中使用
为了保证可移植性,QuartzCore不能使用UIImage、UIColor,仅仅能使用CGImageRef、CGColorRef
UIView和CALayer的选择
通过CALayer,就能做出跟UIView一样的界面效果
既然CALayer和UIView都能实现同样的显示效果,那到底该选择谁好呢?
事实上,对照CALayer,UIView多了一个事件处理的功能。
也就是说。CALayer不能处理用户的触摸事件。而UIView能够
所以,假设显示出来的东西须要跟用户进行交互的话,用UIView;假设不须要跟用户进行交互,用UIView或者CALayer都能够
当然。CALayer的性能会高一些,由于它少了事件处理的功能,更加轻量级
position和anchorPoint介绍
//CALayer有2个非常重要的属性:position和anchorPoint
@property CGPoint position;
用来设置CALayer在父层中的位置
以父层的左上角为原点(0, 0)
@property CGPoint anchorPoint;
称为“定位点”、“锚点”
决定着CALayer身上的哪个点会在position属性所指的位置
以自己的左上角为原点(0, 0)
它的x、y取值范围都是0~1,默认值为(0.5, 0.5),意味着锚点在layer的中间
anchorPoint(示意图)
position和anchorPoint
隐式动画
每个UIView内部都默认关联着一个CALayer,我们可用称这个Layer为Root Layer(根层)
全部的非Root Layer。也就是手动创建的CALayer对象。都存在着隐式动画
什么是隐式动画?
当对非Root Layer的部分属性进行改动时。默认会自己主动产生一些动画效果
而这些属性称为Animatable Properties(可动画属性)
列举几个常见的Animatable Properties:
bounds:用于设置CALayer的宽度和高度。改动这个属性会产生缩放动画
backgroundColor:用于设置CALayer的背景色。改动这个属性会产生背景色的渐变动画
position:用于设置CALayer的位置。改动这个属性会产生平移动画
//能够通过动画事务(CATransaction)关闭默认的隐式动画效果
[CATransaction begin];
[CATransaction setDisableActions:YES];
self.myview.layer.position = CGPointMake(10, 10);
[CATransaction commit];
CALayer图层实例
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *redView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[UIView animateWithDuration:1 animations:^{
//缩放
_imageView.layer.transform = CATransform3DMakeRotation(M_PI, 1, 1, 0);
//平移
_imageView.layer.transform = CATransform3DMakeTranslation(200, 200, 0);
//缩放
_imageView.layer.transform = CATransform3DMakeScale(1, 0.5, 1);
//利用KVC改变形变
NSValue *rotation = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 1, 1, 0)];
[_imageView.layer setValue:rotation forKeyPath:@"transform"];
[_imageView.layer setValue:@M_PI forKeyPath:@"transform.rotation"];
[_imageView.layer setValue:@0.5 forKeyPath:@"transform.scale"];
// 平移x轴
[_imageView.layer setValue:@200 forKeyPath:@"transform.translation.x"];
}];
}
- (void)imageLayer
{
// 圆形裁剪
_imageView.layer.cornerRadius = 50;
// 超出layer边框的全部裁剪掉
_imageView.layer.masksToBounds = YES;
_imageView.layer.borderColor = [UIColor whiteColor].CGColor;
_imageView.layer.borderWidth = 2;
}
- (void)viewLayer
{
// 设置阴影透明度
_redView.layer.shadowOpacity = 1;
// 设置阴影颜色
_redView.layer.shadowColor = [UIColor yellowColor].CGColor;
// 设置阴影圆角半径
_redView.layer.shadowRadius = 10;
// 设置圆角半径
_redView.layer.cornerRadius = 50;
// 设置边框半径
_redView.layer.borderColor = [UIColor whiteColor].CGColor;
// 设置边框半径
_redView.layer.borderWidth = 2;
}
@end
自己定义涂层
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 创建一个图层
CALayer *layer = [CALayer layer];
// 设置尺寸
layer.bounds = CGRectMake(0, 0, 100, 100);
// 设置位置
layer.position = CGPointMake(100, 100);
// 设置颜色
layer.backgroundColor = [UIColor redColor].CGColor;
// 设置内容
layer.contents = (__bridge id)[UIImage imageNamed:@"picture.png"].CGImage;
[self.view.layer addSublayer:layer];
}
@end
iOS开发 - CALayer图层的更多相关文章
- [iOS Animation]-CALayer 图层性能
图层性能 要更快性能,也要做对正确的事情. ——Stephen R. Covey 在第14章『图像IO』讨论如何高效地载入和显示图像,通过视图来避免可能引起动画帧率下降的性能问题.在最后一章,我们将着 ...
- ios开发之图层与核心动画一:图层CALayer的认识
#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutl ...
- [iOS Animation]-CALayer 图层树
图层的树状结构 巨妖有图层,洋葱也有图层,你有吗?我们都有图层 -- 史莱克 Core Animation其实是一个令人误解的命名.你可能认为它只是用来做动画的,但实际上它是从一个叫做Layer Ki ...
- IOS开发-CALayer和UIView详细汇总
1. CALayer和UIView之间的关系: 在iOS系统中,你能看得见摸得着的东西基本上都是UIView,比如UI控件.图标等等,都是UIView. 其实UIView之所以能显示在屏幕上,完 ...
- [iOS Animation]-CALayer 图层几何学
图层几何学 不熟悉几何学的人就不要来这里了 --柏拉图学院入口的签名 在第二章里面,我们介绍了图层背后的图片,和一些控制图层坐标和旋转的属性.在这一章中,我们将要看一看图层内部是如何根据父图层和兄弟图 ...
- ArcGIS Runtime SDK for iOS开发地图图层-图形图层
注:本文翻译自:https://developers.arcgis.com/ios/objective-c/guide/creating-a-graphics-layer.htm 创建图 ...
- iOS开发UI篇—CAlayer(创建图层)
iOS开发UI篇—CAlayer(创建图层) 一.添加一个图层 添加图层的步骤: 1.创建layer 2.设置layer的属性(设置了颜色,bounds才能显示出来) 3.将layer添加到界面上(控 ...
- iOS开发UI篇—CAlayer层的属性
iOS开发UI篇—CAlayer层的属性 一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property ...
- iOS开发UI篇—CALayer简介
iOS开发UI篇—CALayer简介 一.简单介绍 在iOS中,你能看得见摸得着的东西基本上都是UIView,比如一个按钮.一个文本标签.一个文本输入框.一个图标等等,这些都是UIView. 其实 ...
随机推荐
- async [ə'zɪŋk] 函数
ES7提供了async函数,使得异步操作变得更加方便.async函数是什么?一句话,async函数就是Generator函数的语法糖. 例1: var asyncFun = async functio ...
- 【转发】未能加载文件或程序集“Oracle.DataAccess”或它的某一个依赖项。试图加载格式不正确的程序。
http://www.cnblogs.com/joey0210/archive/2012/09/29/2708420.html 上一篇文章说到了DLL引用问题,主要是说的程序中如果使用过了反射, ...
- BOM*创建工艺路线
--工艺路线 DECLARE -- API input variables l_operation_tbl bom_rtg_pub.operation_tbl_type := bom_rtg_pub. ...
- Android开发之定位系统
2013-07-04 定位系统 全球定位系统(Global Positioning System, GPS), 又称全球卫星定位系统. 最少只需其中3颗卫星,就能迅速确定用户组地球所处的位置及海拔高度 ...
- IO多路复用之poll
1.基本知识 poll的机制与select类似,与select在本质上没有多大差别,管理多个描述符也是进行轮询,根据描述符的状态进行处理,但是poll没有最大文件描述符数量的限制.poll和selec ...
- C++二维数组讲解、二维数组的声明和初始化
我们知道,一维空间是一条线,数学中用一条数轴来表达:二维空间是一个平面,数学中用平面坐标系来表达.那么二维数组又是什么样的呢? 线与面 我们用一个下标来描述一维数组中的某个元素,就好像在用数描述一条线 ...
- Spark SQL and DataFrame Guide(1.4.1)——之DataFrames
Spark SQL是处理结构化数据的Spark模块.它提供了DataFrames这样的编程抽象.同一时候也能够作为分布式SQL查询引擎使用. DataFrames DataFrame是一个带有列名的分 ...
- spring in action 7.1 小结
0 AbstractAnnotationConfigDispatcherServletInitializer剖析,在Servlet 3.0环境中,容器会在类路径中查找实现ServletContaine ...
- unity, switch platform
例如一开始是iPhone, iPod Touch and iPad,如图: 想切换成PC, Mac & Linux Standalone,如图: 方法是File->Build Setti ...
- C数组逆序
一.标准交换模式 /**** *标准交换模式 *实现数组的逆序,原理就是数组的首尾元素进行交换 ***/ #define N 5; int main(){ int array[N] = {15,20, ...