IOS View Programming Guide notes
The program we see
For all IOS apps, the content we see is displayed by UIView.
UIView is like a scene, UIWindow is like a stage, and UIView is shown on the stage of UIWindow, so that we can see a variety of UI interfaces. UIWindow itself does not have any content. It only provides a place for these uiviews to be displayed and switched.
Generally, an APP only has one UIWindow as a display place. Multiple uiwindows are used only when multiple screens are needed.
Basic Structure of UIView
1. The UIView is attached to the UIWindow. Only the View on the UIWindow can be seen by us.
2. UIView itself can be displayed as a container and SubView can be managed to enrich the interface.
3. Each UIView has a corresponding Core Animation Layer object for support. A Layer object is a CALayer class object. This object stores information data of the corresponding UIView object and processes the animation operation of the UIView.
With the CALayer object, it mainly provides the following functions:
(1) The CALayer object stores the information data of the corresponding UIView object, which can greatly reduce the rendering times of the UIView, we can directly read the rendered data stored in the CALayer object as much as possible without rendering the UIView every time.
(2) because the CALayer object stores the UIView information, the current content of the UIView can be operated to achieve the animated effect of the UIView.
(3) You can directly operate the CALayer object to achieve richer and more flexible display and animation effects.
Shows the relationships between UIWindow, UIView, and Core Animation Layer objects:
View level and SubView Management
As described above, you can add sub-views to the UIView to form parent-child logical relationships such as superView and subView.
SubView is put into a subView queue by the parent View for management.
Hierarchical relationship displayed by view
The added view blocks the previous view.
Event responder chain)
After the App obtains the user's operation event, it first passes the event to the subview at the top of the event. If the subview does not respond to the event, it will be passed to the superview of the view for response, so that it will continue until the event is responded or discarded by the program and not processed. This is the so-called Event responder chain.
View Content Rendering cycle
In IOS, The UIView class uses an on-demand policy to draw the display content of the View. The on-demand policy means that the system will call your rendering function to re-draw the View content only when you explicitly tell the system to re-paint the VIew content. Otherwise, in most cases, the system only uses the snapshot image of the View content to display the View content.
The specific implementation is as follows:
1. When a View is displayed on the screen for the first time, the system will automatically call the View rendering function to display the content and retain a snapshot image for the content.
2. If the View content does not change, in most cases, the system only uses snapshot images to represent the View content. Note that the system does not take the initiative to ask if the View content has changed. You need to notify the system to change the content to update the View display content.
3. When you change the View content, call
setNeedsDisplayOrsetNeedsDisplayInRect:
Method to notify the system that the content has changed and the View Interface needs to be re-painted.
4. After the system learns that the content has changed, it does not re-paint the View Interface immediately. Instead, when this round of run loop is complete and the re-painting content is prepared, to re-paint the View of the changed content.
5. When the system redraws the View, the process is not uniform. For custom views, we need to rewrite
DrawRect: method.
Of course, you can also change the View content by directly changing the Layer object corresponding to the View.
6. After the system redraws the View, a new content snapshot is collected to represent the View content for display in most of the time.
Note that the geometric deformation of a general View (such as stretching and narrowing, does not cause re-painting of the content, but only the stretching and narrowing of the content snapshot ).
View content Mode
As mentioned above, when the View is displayed on the screen, the system uses a snapshot to represent the content.
In this case, when we change the frame and other attributes of a View, the content (content snapshot) does not necessarily change at the same time. The way the Content is displayed depends on the Content Modes of the UIView object. The default system will adopt the UIViewContentModeScaleToFill mode, so that the Content snapshot is stretched to fill the entire View area. The Content Mode format is as follows:
Note: After the ContentMode of the View is set, each View's geometric deformation will cause the system to call the View's drawRect: Method to re-paint the View. Therefore, this attribute should be avoided, at the same time, we should never use attributes for system views.
Coordinate System of View
The coordinate system of IOS is as follows:
UIWindow and UIView all have their own coordinate systems. The attributes of Frame, Bound, and Center commonly used in UIView are relative to different coordinate systems. The details are as follows:
A view object tracks its size and location using itsframe,bounds, AndcenterProperties:
TheframeProperty contains the frame rectangle, which specifies the size and location of the view in its superview's coordinate system.
TheboundsProperty contains the bounds rectangle, which specifies the size of the view (and its content origin) in the view's own local coordinate system.
ThecenterProperty contains the known center point of the view in the superview's coordinate system.
To sum up, it is the frame of the view. The Coordinate System of the center attribute is for the coordinate system of its superview. The bounds attribute is for its own coordinate system.
The relationship between Frame, Center, and Bounds is relative to the superview Coordinate System of the view, so it can be used as the positioning and size of the subview in the superview. We recommend that you change the Center attribute for displacement movement because the Frame attribute does not exist in some deformation. Frame, Center, and Bounds affect each other as follows:
When you setframeProperty, the size value inboundsProperty changes to match the new size of the frame rectangle. The value incenterProperty similarly changes to match the new center point of the frame rectangle.
When you setcenterProperty, the origin value inframeChanges accordingly.
When you set the size ofboundsProperty, the size value inframeProperty changes to match the new size of the bounds rectangle.
The Runtime interaction model of the View can interact with the view in real time through touch and other actions. In IOS, the interaction model between user actions and views is roughly as follows: 1. When a user touches the screen, the event hardware is identified and sent to the UIKit Framework. 2. The touch event is encapsulated into a UIEvent class object by the UIKit Framework and sent to the corresponding Event Response View. 3. In the event response View, we can use the corresponding functions to capture the current UIEvent event (gesture recognition mechanism or override touch series response functions ). 4. In the View, we customize the code to respond, such as callingsetNeedsLayoutMethod to re-layout the system (that is, to allow the system to actively calllayoutSubviews) CallsetNeedsDisplayOrsetNeedsDisplayInRect:Method to allow the system to redraw the View content. (That is, let the system actively call our rewrittendrawRect:) Function. Modify the properties of the UIView or notify a controller object.