layer 的一些知识
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"];
可以使用的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的其他属性:
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 的一些知识的更多相关文章
- iOS之小功能模块--彩虹动画进度条学习和自主封装改进
前言: 首先展示一下这个iOS小示例的彩色进度条动画效果: 阅读本文先说说好处:对于基础不好的读者,可以直接阅读文末尾的"如何使用彩虹动画进度条"章节,然后将我封装好的这个功能模块 ...
- Android Weekly Notes Issue #283
November 12th, 2017 Android Weekly Issue #283 本期内容包括Gradle相关的几篇,如封装繁杂依赖的技巧,通过kotlin dsl让gradle支持kotl ...
- [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 ...
- Android 12(S) 图形显示系统 - createSurface的流程(五)
题外话 刚刚开始着笔写作这篇文章时,正好看电视在采访一位92岁的考古学家,在他的日记中有这样一句话,写在这里与君共勉"不要等待幸运的降临,要去努力的掌握知识".如此朴实的一句话,此 ...
- Oracle 集群】ORACLE DATABASE 11G RAC 知识图文详细教程之ORACLE集群概念和原理(二)
ORACLE集群概念和原理(二) 概述:写下本文档的初衷和动力,来源于上篇的<oracle基本操作手册>.oracle基本操作手册是作者研一假期对oracle基础知识学习的汇总.然后形成体 ...
- Java基础知识【上】(转载)
http://blog.csdn.net/silentbalanceyh/article/details/4608272 (最终还是决定重新写一份Java基础相关的内容,原来因为在写这一个章节的时候没 ...
- LLDB基础知识
LLDB基础知识 LLDB控制台 Xcode中内嵌了LLDB控制台,在Xcode中代码的下方,我们可以看到LLDB控制台. LLDB控制台平时会输出一些log信息.如果我们想输入命令调试,必须让程序进 ...
- iOS CoreAnimation详解(一) 有关Layer的动画
以前由于项目需要 也写了一些动画 ,但是知识不系统,很散.这段时间趁着项目完成的空袭,来跟着大神的脚步系统的总结一下iOS中Core Animation的知识点. 原博客地址:http://blog. ...
- Docker知识-1
[编者的话]本文用图文并茂的方式介绍了容器.镜像的区别和Docker每个命令后面的技术细节,能够很好的帮助读者深入理解Docker. 这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(co ...
随机推荐
- Dockerfile编写语法
docker镜像本质上就是一个个基础镜像的堆叠,为了做出我们想要的镜像,我们需要考虑最终镜像所需的所有基础环境,然后一层层堆叠.也就是不断以基础镜像搭建上层镜像. 先看例子: # Version: # ...
- 推荐 iOS 网站:
0. https://developer.apple.com/videos/1. http://www.raywenderlich.com2. http://nshipster.com3. http: ...
- - (void)addAnimation:(CAAnimation *)anim forKey:(nullable NSString *)key; 方法浅析
转载自:http://blog.csdn.net/ronaldo_carry/article/details/49070119 将viewdidload里面的代码全部注释掉 - (void)viewD ...
- jenkins tags
List Subversion tags (and more) 参数设置 Tags filter ^((?!_ta_).)*$ 表示不含_ta_ Tags filtertrunk|(tags|bran ...
- 关于c中的inline
在c中,为了解决一些频繁调用的小函数大量消耗栈空间或是叫栈内存的问题,特别的引入了inline修饰符,表示为内联函数.栈空间就是指放置程式的局部数据也就是函数内数据的内存空间,在系统下,栈空间是有限的 ...
- Chapter 1 First Sight——14
I parked in front of the first building, which had a small sign over the door reading front office. ...
- Hibernate分页查询小结
通常使用的Hibernate通常是三种:hql查询,QBC查询和QBE查询: 1.QBE(Qurey By Example)检索方式 QBE 是最简单的,但是功能也是最弱的,QBE的功能不是特别强大, ...
- (转)MATLAB入门教程
MATLAB入门教程 1.MATLAB的基本知识 1-1.基本运算与函数 在MATLAB下进行基本数学运算,只需将运算式直接打入提示号(>>)之後,并按入Enter键即可.例如: ...
- Naive Bayes在mapreduce上的实现(转)
Naive Bayes在mapreduce上的实现 原文地址 http://www.cnblogs.com/sunrye/p/4553732.html Naive Bayes是比较常用的分类器,因为思 ...
- ExtJS4.1自带API打不开的问题解决
在ext官网个下载的最新版本的extjs,本来想看看里面的docs文档的,结果却发现打不开,总是转个不停,于是就打开index.html的源码,看到引入ext的js文件的时候,看到引入的是ext.js ...