UIView与CALayer的区别,很详细

研究Core Animation已经有段时间了,关于Core Animation,网上没什么好的介绍。苹果网站上有篇专门的总结性介绍,可是彷佛原理性的东西很少,看得人云山雾罩,感受,写那篇东西的人,实际上是假设读的人了解界面动画技术的原理的。今天有点别的事情要使用Linux,忘掉了ssh的密码,没办法从新设ssh,结果怎么也想不起来怎么设ssh远程登录了,没办法又到网上查了一遍,太浪费时间了,痛感忘记记笔记是多么可怕的事情。鉴于Core Animation的内容实在是很是繁杂,应用的Obj-C语言自己的特性也不少,因此写个备忘录记录一下,懂的人看了后若是发现了错误,还不吝指教。

1.UIView是iOS系统中界面元素的基础,全部的界面元素都继承自它。它自己彻底是由CoreAnimation来实现的(Mac下彷佛不是这样)。它真正的绘图部分,是由一个叫CALayer(Core Animation Layer)的类来管理。UIView自己,更像是一个CALayer的管理器,访问它的跟绘图和跟坐标有关的属性,例如frame,bounds等等,实际上内部都是在访问它所包含的CALayer的相关属性。

2.UIView有个layer属性,能够返回它的主CALayer实例,UIView有一个layerClass方法,返回主layer所使用的类,UIView的子类,能够经过重载这个方法,来让UIView使用不一样的CALayer来显示,例如经过
1
- (class) layerClass {
2
  return ([CAEAGLLayer class]);
3
}
使某个UIView的子类使用GL来进行绘制。

3.UIView的CALayer相似UIView的子View树形结构,也能够向它的layer上添加子layer,来完成某些特殊的表示。例以下面的代码
1
grayCover = [[CALayer alloc] init];
2
grayCover.backgroundColor = [[[UIColor blackColor] colorWithAlphaComponent:0.2] CGColor];
3
[self.layer addSubLayer: grayCover];
会在目标View上敷上一层黑色的透明薄膜。

4.UIView的layer树形在系统内部,被系统维护着三份copy(这段理解有点吃不许)。
第一份,逻辑树,就是代码里能够操纵的,例如更改layer的属性等等就在这一份。
第二份,动画树,这是一个中间层,系统正在这一层上更改属性,进行各类渲染操做。
第三份,显示树,这棵树的内容是当前正被显示在屏幕上的内容。
这三棵树的逻辑结构都是同样的,区别只有各自的属性。

5.动画的运做
UIView的主layer之外(我以为是这样),对它的subLayer,也就是子layer的属性进行更改,系统将自动进行动画生成,动画持续时间有个缺省时间,我的感受大概是0.5秒。在动画时间里,系统自动断定哪些属性更改了,自动对更改的属性进行动画插值,生成中间帧而后连续显示产生动画效果。

6.坐标系系统(对position和anchorPoint的关系仍是犯晕)
CALayer的坐标系系统和UIView有点不同,它多了一个叫anchorPoint的属性,它使用CGPoint结构,可是值域是0~1,也就是按照比例来设置。这个点是各类图形变换的坐标原点,同时会更改layer的position的位置,它的缺省值是{0.5, 0.5},也就是在layer的中央。
某layer.anchorPoint = CGPointMake(0.f, 0.f);
若是这么设置,layer的左上角就会被挪到原来的中间的位置,
加上这样一句就行了
某layer.position = CGPointMake(0.f, 0.f);

7.真实例子的分析

   
这是iphone上iBook翻页的效果,假设每一页都是一个UIView,我以为一个页面是贴了俩个Layer,文字Layer显示正面的内容,背面layer用文字layer的快照作affine翻转,贴在文字layer的后面。由于Layer能够设置显示阴影,也许后面的阴影效果没有使用单独的一个layer来显示。至于这个曲面效果,我查了不少资料也没有结果,估计是使用了GL的曲面绘图?

8.最后一个让人恶心的。

layer能够设置圆角显示,例如UIButton的效果,也能够设置阴影显示,可是若是layer树中的某个layer设置了圆角,树中全部layer的阴影效果都将显示不了了。若是既想有圆角又想要阴影,好像只能作两个重叠的UIView,一个的layer显示圆角,一个的layer显示阴影..... windows


1)老祖 app

万物归根,UIView和CALayer都是的老祖都是NSObjet。 框架

 

1: UIView的继承结构为: UIResponder : NSObjectiview

 

能够看出UIView的直接父类为UIResponder 类, UIResponder 是gsm的呢? ssh

官方的解释: iphone

The UIResponder class defines an interface for objects that respond to and handle events. It is the superclass of UIApplicationUIView and its subclasses (which include UIWindow). Instances of these classes are sometimes referred to as responder objects or, simply, responders. ide

 

The UIView class defines a rectangular area on the screen and the interfaces for managing the content in that area. At runtime, a view object handles the rendering of any content in its area and also handles any interactions with that content. The UIView class itself provides basic behavior for filling its rectangular area with a background color. More sophisticated content can be presented by subclassing UIView and implementing the necessary drawing and event-handling code yourself. The UIKit framework also includes a set of standard subclasses that range from simple buttons to complex tables and can be used as-is. For example, a UILabelobject draws a text string and a UIImageView object draws an image. 布局

 

可见 UIResponder是用来响应事件的,也就是UIView能够响应用户事件。 动画

 

2:CALayer的继承结构为: NSObject 网站

 

直接从 NSObject继承,由于缺乏了UIResponder类,因此CALayer悲催的不能响应任何用户事件。

 

The CALayer class is the model class for layer-tree objects. It encapsulates the position, size, and transform of a layer, which defines its coordinate system. It also encapsulates the duration and pacing of a layer and its animations by adopting the CAMediaTiming protocol, which defines a layer’s time space.

 

从官方的解释能够看出,CALayer定义了position、size、transform、animations 等基本属性。那UIView的size、frame、position这些属性是从那里来的呢?上面的官方解释没有说明这一点,咱们一会再分析

 

至此咱们了解到了,UIView 和CALayer的基本信息和主要负责处理的事情。

 

(2)所属框架

1:UIView是在 /System/Library/Frameworks/UIKit.framework中定义的。

这个又是作什么的呢?

The UIKit framework provides the classes needed to construct and manage an application’s user interface for iOS. It provides an application object, event handling, drawing model, windows, views, and controls specifically designed for a touch screen interface.

 

可见UIKit主要是用来构建用户界面,而且是能够响应事件的(得意与UIView的父类UIResponder,至于UIResponderd的实现原理不是此次分析的目的,在此不作过多的解释

 

在这里思考一个问题UIView既然是构建用户界面的,那他是经过什么方式绘制这些图片、文字之类的信息的呢? 

 

Ios中的2D图像绘制都是经过QuartzCore.framework实现的。难道是经过QuartzCore.framework实现的?那又是经过什么方式和QuartzCore.framework联系起来的呢??咱们一会再看。

 

2:CALayer是在/System/Library/Frameworks/QuartzCore.framework定义的。并且CALayer做为一个低级的,能够承载绘制内容的底层对象出如今该框架中。

 

 

如今比较一下uiview和calayer均可以显示图片文字等信息。难道apple提供了,两套绘图机制吗?不会。

UIView相比CALayer最大区别是UIView能够响应用户事件,而CALayer不能够。UIView侧重于对显示内容的管理,CALayer侧重于对内容的绘制。

你们都知道QuartzCore是IOS中提供图像绘制的基础库。而且CALayer是定义该框架中。难道UIView的底层实现是CALayer??

 

官方作出了明确的解释:

Displaying Layers in Views

Core Animation doesn't provide a means for actually displaying layers in a window, they must be hosted by a view. When paired with a view, the view must provide event-handling for the underlying layers, while the layers provide display of the content.

The view system in iOS is built directly on top of Core Animation layers. Every instance of UIView automatically creates an instance of a CALayer class and sets it as the value of the view’s layer property. You can add sublayers to the view’s layer as needed.

On Mac OS X you must configure an NSView instance in such a way that it can host a layer.

 

因而可知UIView是基于CALayer的高层封装。The view system in iOS is built directly on top of Core Animation layers. 

 

UIView 的方法:

layerClass - Implement this method only if you want your view to use a different Core Animation layer for its backing store. For example, if you are using OpenGL ES to do your drawing, you would want to override this method and return the CAEAGLLayer class.

该方法保留了UIView的本质。即对UIView所管理的内容,任何显示也是受到CALayer的影响的。

 

  1. (3)类似支持

1:类似的树形结构

2:显示内容绘制方式

3: 布局约束


  1. (4) UIView 是什么,作什么

UIView是用来显示内容的,能够处理用户事件


  1. (5)CALayer是什么,作什么

CALayer是用来绘制内容的,对内容进行动画处理依赖与UIView来进行显示,不能处理用户事件。


  1. (6)为什么有两套结构

并非两套体系,UIView和CALayer是相互依赖的关系。UIView依赖与calayer提供的内容,CALayer依赖uivew提供的容器来显示绘制的内容。归根到底CALayer是这一切的基础,若是没有CALayer,UIView自身也不会存在,UIView是一个特殊的CALayer实现,添加了响应事件的能力。

 

  1. (7)二者之间的关系

发之于肤,血之于肉,灵之于魄,男人之于肾的关系。依存的关系

 

结论:

UIView来自CALayer,高于CALayer,是CALayer高层实现与封装。UIView的全部特性来源于CALayer支持。

相关文章
相关标签/搜索