The View Hierarchy

Every application has a single instance of UIWindow that serves as the container for all the views in the application. The window is created when the application launches. Once the window is created, you can add other views to it.

When a view is added to the window, it is said to be a subview of the window. Views that are subviews of the window can also have subviews, and the result is a hierarchy of view objects with the window at its root.

Figure 4.3  An example view hierarchy and the interface that it creates

An example view hierarchy and the interface that it creates

Once the view hierarchy has been created, it will be drawn to the screen. This process can be broken into two steps:

  • Each view in the hierarchy, including the window, draws itself. It renders itself to its layer, which is an instance of CALayer. (You can think of a view’s layer as a bitmap image.)

  • The layers of all the views are composited together on the screen.

Figure 4.4 shows another example view hierarchy and the two drawing steps.

Figure 4.4  Views render themselves and then are composited together

Views render themselves and then are composited together

Classes like UIButton and UILabel already know how to render themselves to their layers. For instance, in Quiz, you created instances of UILabel and told them what text to display, but you did not have to tell them how to draw text. Apple’s developers took care of that.

Apple, however, does not provide a class whose instances know how to draw concentric circles. Thus, for Hypnosister, you are going to create your own UIView subclass and write custom drawing code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset