CALayer 图层

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, andtranslation 

 
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 the shadowPath 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的方法。


14.

CALayer和UIView的关系:

 在UIView中有一个layer属性作为根图层,根图层上可以放其他子图层,在UIView中所有能够看到的内容都包含在layer中

 CALayer负责视图中显示的内容和动画

 UIView负责监听和响应事件

 CALayer:包含在QuartzCore框架中,QuartzCore既可以用在iOS中又可以用在Mac OS X中

 使用Core Animation开发动画的本质就是将CALayer中的内容转化为位图从而供硬件操作,所以要熟练掌握动画操作必须先来熟悉CALayer

 -----------------------------------

 CALayer的存在意义:

 在iOS中CALayer的设计主要是了为了内容展示和动画操作,CALayer本身并不包含在UIKit中,它不能响应事件(咱们已经测试过)

 由于CALayer在设计之初就考虑它的动画操作功能,CALayer很多属性在修改时都能形成动画效果,这种属性称为“隐式动画属性”。

 但是对于UIView的根图层而言属性的修改并不形成动画效果,因为很多情况下根图层更多的充当容器的做用,如果它的属性变动形成动画效果会直接影响子图层。

 另外,UIView的根图层创建工作完全由iOS负责完成,无法重新创建,但是可以往根图层中添加子图层或移除子图层

 --------------------------------------

 CALayer的常用属性

 定义一个图层的位置

 bounds:本身的尺寸

 position:中心点 是layer相对于self.view.layer的 一个点

类似:船靠岸   钉在岸边上的那个点

 属性 说明  是否支持隐式动画

 anchorPoint 锚点、定位点  锚点的描述是相对于 *自己* x、y位置比例而言的 默认在图像中心点(0.5,0.5)的位置  决定图层的哪一个点 显示在中心点的位置

 backgroundColor 图层背景颜色

 borderColor 边框颜色

 borderWidth 边框宽度

 bounds 图层大小

 contents 图层显示内容,例如可以将图片作为图层内容显示

 contentsRect 图层显示内容的大小和位置

 cornerRadius 圆角半径

 doubleSided 图层背面是否显示,默认为YES

 frame 图层大小和位置,不支持隐式动画,所以CALayer中很少使用frame,通常使用bounds和position代替

 hidden 是否隐藏

 mask 图层蒙版

 maskToBounds 子图层是否剪切图层边界,默认为NO

 opacity 透明度 ,类似于UIView的alpha

 position 决定图层在父视图的位置 图层位于 *父视图* 中心点位置,类似于UIView的center

 shadowColor 阴影颜色

 shadowOffset 阴影偏移量

 shadowOpacity 阴影透明度,注意默认为0,如果设置阴影必须设置此属性

 shadowPath 阴影的形状

 shadowRadius 阴影模糊半径

 sublayers 子图层

 sublayerTransform 子图层形变

 transform 图层形变

 以上支持隐式动画的属性 本质是这些属性的变动默认隐含了CABasicAnimation动画实现

 注意:rootLayer不支持隐式动画 也就是UIView上的根Layer 如self.view.layer

 anchorPoint属性是图层的锚点,范围在(0~1,0~1) anchorPoint默认为(0.5,0.5) 表示在x、y轴的比例,这个点永远与position(中心点)重合,当图层中心点固定后,调整anchorPoint即可达到调整图层显示位置的作用(因为它永远和position重合)

原文地址:https://www.cnblogs.com/woaixixi/p/4568714.html