layer类似于ps的图层,如果把一个uiview看做图片的画,layer就像是图层.一个图片是由很多个大小不同的有层次的图层构成的,uiview也是.

1. 一个view有一个underlying layer,它是这个view所有的draw的实现者,可通过view的layer property获得,但是它没有相应的view property,相反的,view是这个layer的delegate.

如果想改变view的underlying layer, 需实现view的 +(class) layerClass{}函数,返回自定义的layer类.往往这个类不负责具体的画图,只是负责组织多个子layer的显示.

view和layer的关系:view通过layer画图,layer缓存了所有的绘图;如此,layer可以被用来操作改变view的外观,而不用每次都需要view去绘制自己.这就是contentstretchmode的机理,当view的bounds改变时,ios的绘图系统只是简单的拉伸和重新排列缓存的layer图像.

2.layer的层级
layer的层级和view相似,一个layer有多个sublayer,一个sublayer只有一个最近的superlayer.

有一些列的函数控制layer的层级顺序,同view想似:addsublayer;insertsublayer:atindex(below/above);
replacesublayer:with; removefromsuperlayer.

同uiview的subviews property不同,layer的sublayers是可写的.因此可以一次性改变layer的多个sublayers;如果要移除所有的,只需置nil即可.

layer同时也有zposotion的属性,拥有低的zposition的layer比高的先绘制.当有时使用sublayer不便控制层级时,使用zpositon是个很好的选择.

3.layer的位置
控制layer的位置使用两个property.
position 一个superlayer坐标系下的坐标;
anchorpoint 一个本身坐标系下的坐标;
如同属性名称一样,就像用图钉在墙上订一个图片,position表示的是图钉在墙上坐标,anchorpoint表示是图钉在图片上坐标.

4.CAScrollLayer
使用cascrolllayer可以实现:通过改变一个layer的origin坐标,显示它的一部分内容.它的maskstobounds属性一般设为yes,这样就只能看见它的bounds内的内容.
可以通过调用cascrolllayer本身的函数或者调用它的sublayer函数实现layer的滚动功能
Talking to the superlayer (the CAScrollLayer)
scrollToPoint: changes the CAScrollLayer’s bounds origin to that point. scrollTo- Rect: changes the CAScrollLayer’s bounds origin minimally so that the given por- tion of the bounds rect is visible.
Talking to a sublayer
scrollPoint: changes the CAScrollLayer’s bounds origin so that the given point of the sublayer is at the top left of the CAScrollLayer. scrollRectToVisible: changes the CAScrollLayer’s bounds origin so that the given rect of the sublayer’s bounds is within the CAScrollLayer’s bounds area. You can also ask the sublayer for its visibleRect, the part of this sublayer now within the CAScrollLayer’s bounds.

5.layout
当一个layer的bounds发生改变或者显式调用了setNeedsLayout,它的layoutsublayers函数被调用,因此需要重写该函数实现layout.同时也可以在delegate中重写layoutsublayersoflayer,比如在uiview中为实现其underlying layer的layout,实现该函数.

layout的key-value codeing:

6.drawing in a layer

layer有一个content property,类似于uiimageview的image.但是它的类型是cgimageref.

同uiview的drawrect:类似,可以通过实现如下四个函数来提供layer的content或者重绘layer.

display in a subclass
Your CALayer subclass can override display. There’s no graphics context at this point, so display is pretty much limited to setting the contents.

drawInContext: in a subclass
Your CALayer subclass can override drawInContext:. The parameter is a graphics context into which you can draw directly; the discussion of drawing from Chap- ter 15 thus pertains.

displayLayer: or drawLayer:inContext: in the delegate
You can set the CALayer’s delegate property, and implement displayLayer: or drawLayer:inContext:. They are parallel to display and drawInContext:, the former providing no graphics context so that it’s fit mostly for setting the contents, and the latter providing a graphics context into which you can draw directly.

不能改变underlying layer的delegate.
尽量不要让layer重新绘制自己,除非你自己想要这么做

当一个uiview调用了setneessdisplay,他的underlying layer被也被发送了setneedsdisplay函数,除非该view没有任何drawrect的实现(这种情况下,默认view不需要重绘).因此如果需要view重绘时underlying layer同时重绘,至少需要实现一个空的drawrect:.

7. layer的contents resizing and positioning
一旦layer有了contents,不管是通过设置content还是通过draw获得的,都有如下属性:

contentsGravity:类似于uiview的contentsmode,是拉伸满整个view的bounds,还是在view上居中显示.

contentsRect:用于表述将要绘制的contents image的部分内容.使用这个属性,可以很方便的绘制一个大图的一部分,而不需要重绘或者改变contents image.同样可以使用contents rect缩小contents image,只需要把contents rect设置的比contents大,为了防止边缘效应,需要给contengs image一个清晰的像素边框

contentsCenter:用于描述当contentsgravity被设置成拉伸状态时,拉伸的中心.

needsdisplayonboundschange:yes说明bounds发生改变时重绘,无关contentsgravity;NO说明bounds发生改变时不重绘,只是利用cache的image采用contentgravity以及参考contentscenter拉伸.

8.系统的一些拥有自己绘制功能的layer.

CATextLayer:
catextlayer拥有string property,可以是NSString或NSAttributedString,以及其他的format的property,然后她负责绘制string.text和contents的是冲突的,因此一般不对catextlayer设置contents.
catextlayer可以实现uilabel不能实现的功能;以为使用了nsattributedstring,可以显示不同大小,字体,下划线等的文字.

CAShapeLayer
它有一个path的property,CGPath,可以填充或者画出,或者同时填充和画出响应的path.

CAGradientLayer
将背景简单的填上一个线性的放射图样.

9.Transform

CATransform3D:该transform发生在anchorPoint处延伸的三维空间中.
对于应用在catransform3d上的角度变化,需要提供一个坐标描述角度变化发生时,参考的向量.顺时针是从逆着参考方向向量的方向看过去的.

为了做出透视变换,需要将layer的正交投射和其superlayer的投射变换结合起来,才能做出实际的透视效果。

However, there’s a widely used trick for introducing a quality of perspective into the way layers are drawn: make them sublayers of a layer whose sublayerTransform prop- erty maps all points onto a “distant” plane. (This is probably just about the only thing thesublayerTransform property is ever used for.) Combined with orthographic projec- tion, the effect is to apply one-point perspective to the drawing, so that things do get perceptibly smaller in the negative z-direction.

g.anchorPoint = CGPointMake(1,0.5);
g.position = CGPointMake(CGRectGetMaxX(self.bounds), CGRectGetMidY(self.bounds)); g.transform = CATransform3DMakeRotation(M_PI/4.0, 0, 1, 0);
CATransform3D transform = CATransform3DIdentity;
transform.m34 = -1.0/1000.0;
self.sublayerTransform = transform;

10. CATransformLayer
这个layer本身不实现任何绘制,它仅仅是为了实现有层次的layer的一个载体。它的sublayers本身就有一个depth的概念。详情见p351. 
 
11. Transforms and Key-Value Coding
 
使用transform时,可以使用key-value的模式

g.transform = CATransform3DMakeRotation(M_PI/4.0, 0, 1, 0);

等价于

[g setValue:[NSNumber numberWithFloat:M_PI/4.0] forKeyPath:@"transform.rotation.y"];

可以使用的transform keyvalue coding 有:

rotation.x, rotation.y, rotation.z, rotation (same as rotation.z), scale.x, scale.y, scale.z, translation.x, translation.y, translation.z, and translation

 

12. layer的其他属性:

 
shadow

A layer can have a shadow, defined by its shadowColor, shadowOpacity, shadowRadius, and shadowOffset properties. To make the layer draw a shadow, set the shadow- Opacity to a nonzero value. The shadow is normally based on the shape of the layer’s nontransparent region, but deriving this shape can be calculation-intensive (so much so that in early versions of iOS, layer shadows weren’t implemented). You can vastly improve performance by defining the shape yourself and assigning this shape as a CGPath to theshadowPath property.

border and corner

A layer can have a border (borderWidth, borderColor); the borderWidth is drawn inward from the bounds, potentially covering some of the content unless you compensate.

A layer can be bounded by a rounded rectangle, by giving it a cornerRadius greater than zero. If the layer has a backgroundColor, that background is clipped to the shape of the rounded rectangle. If the layer has a border, the border has rounded corners too.

光栅化rasterize

If a layer is complex (perhaps with shadow, sublayers, and so forth) and if this seems to be a performance drain (especially when scrolling or animating the layer), you may be able to gain some efficiency by “freezing” the entirety of the layer’s drawing as a bitmap. In effect, you’re drawing everything in the layer to a secondary cache and using the cache to draw to the screen. To do this, set the layer’s shouldRasterize to YES and its rasterizationScale to some sensible value (probably [UIScreen main- Screen].scale). You can always turn rasterization off again by setting should- Rasterize to NO, so it’s easy to rasterize just before some massive or sluggish rear- rangement of the screen and then unrasterize afterward. (In addition, you can get some cool “out of focus” effects by setting the rasterizationScale to around 0.3.)

13.layer的key-value coding(KVC)

layer没有tag属性,但是它与kvc属性兼容,因此可以实现它的 defaultValueForKey: class method 静态方法,返回一个自定义的key值,需要时返回即可。setkey:forvalue    valueforkey可以实现不使用成员变量和tag的方法。
 
转自:http://blog.sina.com.cn/s/blog_5100d02501017ond.html

 

layer 的一些知识的更多相关文章

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

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

  2. Android Weekly Notes Issue #283

    November 12th, 2017 Android Weekly Issue #283 本期内容包括Gradle相关的几篇,如封装繁杂依赖的技巧,通过kotlin dsl让gradle支持kotl ...

  3. [ArcGIS API for JavaScript 4.8] Sample Code-Get Started-MapView,SceneView简介

    [官方文档:https://developers.arcgis.com/javascript/latest/sample-code/index.html]  一.Intro to MapView(2D ...

  4. Android 12(S) 图形显示系统 - createSurface的流程(五)

    题外话 刚刚开始着笔写作这篇文章时,正好看电视在采访一位92岁的考古学家,在他的日记中有这样一句话,写在这里与君共勉"不要等待幸运的降临,要去努力的掌握知识".如此朴实的一句话,此 ...

  5. Oracle 集群】ORACLE DATABASE 11G RAC 知识图文详细教程之ORACLE集群概念和原理(二)

    ORACLE集群概念和原理(二) 概述:写下本文档的初衷和动力,来源于上篇的<oracle基本操作手册>.oracle基本操作手册是作者研一假期对oracle基础知识学习的汇总.然后形成体 ...

  6. Java基础知识【上】(转载)

    http://blog.csdn.net/silentbalanceyh/article/details/4608272 (最终还是决定重新写一份Java基础相关的内容,原来因为在写这一个章节的时候没 ...

  7. LLDB基础知识

    LLDB基础知识 LLDB控制台 Xcode中内嵌了LLDB控制台,在Xcode中代码的下方,我们可以看到LLDB控制台. LLDB控制台平时会输出一些log信息.如果我们想输入命令调试,必须让程序进 ...

  8. iOS CoreAnimation详解(一) 有关Layer的动画

    以前由于项目需要 也写了一些动画 ,但是知识不系统,很散.这段时间趁着项目完成的空袭,来跟着大神的脚步系统的总结一下iOS中Core Animation的知识点. 原博客地址:http://blog. ...

  9. Docker知识-1

    [编者的话]本文用图文并茂的方式介绍了容器.镜像的区别和Docker每个命令后面的技术细节,能够很好的帮助读者深入理解Docker. 这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(co ...

随机推荐

  1. C语言头文件

    最近在工作当中遇到了一点小问题,关于C语言头文件的应用问题,主要还是关于全局变量的定义和声明问题.学习C语言已经有好几年了,工作使用也近半年了,但是对于这部分的东西的确还没有深入的思考过.概念上还是比 ...

  2. java synchronized 线程同步机制详解

    Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchronized(this ...

  3. selenium RC

    http://blog.sina.com.cn/s/blog_68cb48770100v9c7.html http://www.cnblogs.com/hyddd/archive/2009/05/24 ...

  4. Nape 获取碰撞点加特效

    package { import nape.phys.Body; import nape.shape.Shape; import nape.shape.Circle; import flash.dis ...

  5. PAT (Advanced Level) 1115. Counting Nodes in a BST (30)

    简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

  6. 求交集,差集,并集,善用java的set

    当有题目有求这些结果时,使用集合数据结构还是很快的.需要考虑的是,注意map和set的区别. public static void main(String[] args) { Set<Integ ...

  7. 代码异味---Code smell

    程序员应该竭尽全力去写那些重复的代码.以下几点是我目前最需要改进的地方. 重复代码: 相同或者相似的代码存在于一个以上的地方. 长方法: 一个非常长的方法.函数或者过程. 巨类: 一个非常庞大的类. ...

  8. MQ日常维护操作手册

    假设队列管理器为QMgrName,以下所有使用QMgrName的地方您都可以替换成您维护的mq队列管理器名称. 一.MQ的启动与停止 用root用户启/停需要root用户包含在mqm组中. 1.MQ的 ...

  9. redis 持久化与备份策略 【转载】

    本文转载自 http://blog.csdn.net/is_zhoufeng/article/details/10210353 持久化(persistence) 本文是 Redis 持久化文档 的中文 ...

  10. wind7系统修改host

    http://jingyan.baidu.com/article/e5c39bf56564a539d7603312.html 由于软件注册的原因,我需要更改hosts文件来防止服务器验证!那么在我们修 ...