原文:http://blog.sina.com.cn/s/blog_5100d02501017ond.html
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;
g.transform = CATransform3DMakeRotation(M_PI/4.0, 0, 1, 0);
等价于
[g setValue:[NSNumber numberWithFloat:M_PI/4.0] forKeyPath:@"transform.rotation.y"];
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
A layer can have a shadow, defined by its shadowColor, shadowOpacity, shadowRadius, and shadowOffsetproperties. 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 cornerA 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.
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)