layer能夠設置圓角顯示,例如UIButton的效果,也能夠設置陰影顯示,可是若是layer樹中的某個layer設置了圓角,樹中全部layer的陰影效果都將顯示不了了。若是既想有圓角又想要陰影,好像只能作兩個重疊的UIView,一個的layer顯示圓角,一個的layer顯示陰影..... windows
1)老祖 app
萬物歸根,UIView和CALayer都是的老祖都是NSObjet。 框架
1: UIView的繼承結構爲: UIResponder : NSObject。 iview
能夠看出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 UIApplication, UIView 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:類似的樹形結構
2:顯示內容繪製方式
3: 佈局約束
UIView是用來顯示內容的,能夠處理用戶事件
CALayer是用來繪製內容的,對內容進行動畫處理依賴與UIView來進行顯示,不能處理用戶事件。
並非兩套體系,UIView和CALayer是相互依賴的關係。UIView依賴與calayer提供的內容,CALayer依賴uivew提供的容器來顯示繪製的內容。歸根到底CALayer是這一切的基礎,若是沒有CALayer,UIView自身也不會存在,UIView是一個特殊的CALayer實現,添加了響應事件的能力。
發之於膚,血之於肉,靈之於魄,男人之於腎的關係。依存的關係
結論:
UIView來自CALayer,高於CALayer,是CALayer高層實現與封裝。UIView的全部特性來源於CALayer支持。