標籤:
(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支援。
[轉]UIView 和 CALayer的那點事