[转]UIView 和 CALayer的那点事

(1)老祖

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

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

可以看出UIView的直接父类为UIResponder 类, UIResponder 是gsm的呢?

官方的解释:

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

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支持。

时间: 2024-08-02 14:19:42

[转]UIView 和 CALayer的那点事的相关文章

UIView和CALayer的区别

1. UIView是iOS系统中界面元素的基础,所有的界面元素都是继承自它.它本身完全是由CoreAnimation来实现的.它真正的绘图部分,是由一个CALayer类来管理.UIView本身更像是一个CALayer的管理器,访问它的跟绘图和跟坐标有关的属性,例如frame,bounds等,实际上内部都是在访问它所包含的CALayer的相关属性. 2. UIView有个重要属性layer,可以返回它的主CALayer实例. // 要访问层,读取UIView实例的layer属性 CALayer *

【好程序员笔记分享】——UIView与CALayer详解

-iOS培训,iOS学习-------型技术博客.期待与您交流!------------ UIView与CALayer详解 研究Core Animation已经有段时间了,关于Core Animation,网上没什么好的介绍.苹果网站上有篇专门的总结性介绍,但是似乎原理性的东西不多,看得人云山雾罩,感觉,写那篇东西的人,其实是假 设读的人了解界面动画技术的原理的.今天有点别的事情要使用Linux,忘掉了ssh的密码,没办法重新设ssh,结果怎么也想不起来怎么设ssh远程登 陆了,没办法又到网上查

UIView与CALayer的区别,很详细

研究Core Animation已经有段时间了,关于Core Animation,网上没什么好的介绍.苹果网站上有篇专门的总结性介绍,但是似乎原理性的东西不多,看得人云山雾罩,感觉,写那篇东西的人,其实是假设读的人了解界面动画技术的原理的.今天有点别的事情要使用Linux,忘掉了ssh的密码,没办法重新设ssh,结果怎么也想不起来怎么设ssh远程登陆了,没办法又到网上查了一遍,太浪费时间了,痛感忘记记笔记是多么可怕的事情.鉴于Core Animation的内容实在是非常繁杂,应用的Obj-C语言

iOS开发——UIView与CALayer详解

UIView与CALayer详解 研究Core Animation已经有段时间了,关于Core Animation,网上没什么好的介绍.苹果网站上有篇专门的总结性介绍,但是似乎原理性的东西不多,看得人云山雾罩,感觉,写那篇东西的人,其实是假 设读的人了解界面动画技术的原理的.今天有点别的事情要使用Linux,忘掉了ssh的密码,没办法重新设ssh,结果怎么也想不起来怎么设ssh远程登 陆了,没办法又到网上查了一遍,太浪费时间了,痛感忘记记笔记是多么可怕的事情.鉴于Core Animation的内

UIView和CALayer的选择

UIView和CALayer的选择 如显示图片的层可以用一个UIImageView来实现. 既然CALayer和UIView都能实现相同的显示效果,那究竟该选择谁好呢? 其实,对比CALayer,UIView多了一个事件处理的功能.也就是说,CALayer不能处理用户的触摸事件,而UIView可以. 所以,在选择的过程中,需要考虑到实际的情况,如果显示出来的东西需要跟用户进行交互的话,用UIView:如果不需要跟用户进行交互,用UIView或者CALayer都可以 当然,CALayer的性能会高

另类的视角看“UIView”和“CALayer”——灵之于魄,男人之于肾的关系

清晨接连翻阅了几篇关于UIView和CALayer的博文,要么是上来一排排的代码(破坏了优雅的气氛),要么是题不达意(喂,what are you ‘写啥’ 嘞),看的我是雨里雾里,当然也有大牛辛辛苦苦的写的优秀文章,只是最近小编也正在学习“核心动画”,因为核心动画操作的对象不是UIView,而是CALayer,而对于二者那剪不断理还乱的关系 小编索性自己通过这篇博文整理了些粗浅的想法. -=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

iOS:UIView的CALayer基本演练

UIView的CALayer基本演练的属性和注意事项: 在UIView中创建一个按钮UIButton,然后设置UIButton的Layer属性 –圆角.边框.阴影及3D形变属性 注意: 1.在UIView中CALayer只是一个类声明,因此需要添加QuartzCore框架(iOS7中已经不需要在导入了) 2.UIKit框架只能应用在iOS而不能用于Mac,但是Quartz 2D是可以跨平台的,因此在使用颜色时,不能直接使用UIColor而需要将颜色转成CGColor 3.修改图层相当于修改UIV

iOS 中 UIView 和 CALayer 的关系

UIView 有一个名叫 layer ,类型为 CALayer 的对象属性,它们的行为很相似,主要区别在于:CALayer 继承自 NSObject ,不能够响应事件. 这是因为 UIView 除了负责响应事件 ( 继承自 UIReponder ) 外,它还是一个对 CALayer 的底层封装.可以说,它们的相似行为都依赖于 CALayer 的实现,UIView 只不过是封装了它的高级接口而已. 那 CALayer 是什么呢? CALayer(图层) 文档对它定义是:管理基于图像内容的对象,允许

UIView和CALayer有什么关系

view是对layer的一种封装,你对view的很多操作事实上是对layer的操作,之所以会出现这两个东西是因为1.view支持很多手势的交互,你所操作iphone的各种点击,拖动等等.2.layer来至于mac os,是可以跨平台的东西.这里就是个很要学问的东西,系统的可变部分和不可变部分,可变部分越多,系统越不稳定,但是功能就更加丰富.layer就是作为一种不可变的东西存在,view作为一种可变的东西存在,所有我们每次在ios更新时候获得了大量的view新特性,但是整个view系统底层很多东