UIView的组成
UIView是iOS系统中界面元素的基础,所有的界面元素都是继承自它。
从官方文档我们可以了解,UIView是继承自UIResponder
UIView.png
官方文档
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.
UIView是在屏幕上提供一个矩形的区域用来显示内容和进行与用户的交互。我们可以从UIResponder文档中了解到UIView所提供的交互就是由UIResponder所提供的。The Responder Chain 会对我们与机器的交互Touch events和Motion and remote control events进行处理。
这样我们了解了为什么UIView能处理点击,滑动等事件了,那么UIVIew是用哪个东西进行矩形区域进行渲染的呢。
我们通过查看UIView所包含的属性可以看到一个默认都有的主layer,查看可知
@property(nonatomic,readonly,retain) CALayer *layer; // returns view's layer. Will always return a non-nil value. view is layer's delegate
又根据官方文档
CALayer: A layer’s main job is to manage the visual content that you provide but the layer itself has visual attributes that can be set, such as a background color, border, and shadow. In addition to managing visual content, the layer also maintains information about the geometry of its content (such as its position, size, and transform) that is used to present that content onscreen.
可以看到其实我们view里面的position, size, and transform等都是由CALayer提供的
所以这个只读的layer负责给UIView 提供绝大多数的数据和图像渲染
所以其实UIView是对于对CALayer(Core Animation Layer)和ResponderChain的高层封装。
网友评论