天天看點

UIView 和 CALayer的那點事

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的影響的。

(3)相似支援

1:相似的樹形結構

2:顯示内容繪制方式

3: 布局限制

(4) UIView 是什麼,做什麼

UIView是用來顯示内容的,可以處理使用者事件

(5)CALayer是什麼,做什麼

CALayer是用來繪制内容的,對内容進行動畫處理依賴與UIView來進行顯示,不能處理使用者事件。

(6)為何有兩套結構

并不是兩套體系,UIView和CALayer是互相依賴的關系。UIView依賴與calayer提供的内容,CALayer依賴uivew提供的容器來顯示繪制的内容。歸根到底CALayer是這一切的基礎,如果沒有CALayer,UIView自身也不會存在,UIView是一個特殊的CALayer實作,添加了響應事件的能力。

(7)兩者之間的關系

發之于膚,血之于肉,靈之于魄,男人之于腎的關系。依存的關系

結論:

UIView來自CALayer,高于CALayer,是CALayer高層實作與封裝。UIView的所有特性來源于CALayer支援。