Most of the things you might want to do visually are donewith view objects—instances of the UIView class.A view object defines a rectangular region on the screen and handles thedrawing and touch events in that region. A view can also act as a parent forother views and coordinate the placement and sizing of those views. TheUIViewclass does most of thework in managing these relationships between views, but you can also customizethe default behavior as needed.
Windows do not have any visible content themselves but provide a basic container for your application’s views
Views are responsible for
- drawing content
- Drawing involves using graphics technologies such as Core Graphics, OpenGL ES, or UIKit to draw shapes, images, and text inside a view’s rectangular area
- handling multitouch events
- A view responds to touch events in its rectangular area either by using gesture recognizers or by handling touch events directly
- managing the layout of any subviews.
- In the view hierarchy, parent views are responsible for positioning and sizing their child views and can do so dynamically
Views work in conjunction with Core Animation layers to handle the rendering and animating of a view’s content. Every view in UIKit is backed by a layer object (usually an instance of the CALayer class), which manages the backing store for the view and handles view-related animations. Most operations you perform should be through the UIView interface. However, in situations where you need more control over the rendering or animation behavior of your view, you can perform operations through its layer instead.
Every view has a corresponding layer object that can be accessed from that view’s layer property. (Because a bar button item is not a view, you cannot access its layer directly.) Behind those layer objects are Core Animation rendering objects and ultimately the hardware buffers used to manage the actual bits on the screen
The use of Core Animation layer objects has important implications for performance. The actual drawing code of a view object is called as little as possible, and when the code is called, the results are cached by Core Animation and reused as much as possible later. Reusing already-rendered content eliminates the expensive drawing cycle usually needed to update views. Reuse of this content is especially important during animations, where the existing content can be manipulated. Such reuse is much less expensive than creating new content.
Views work in conjunction with Core Animation layers tohandle the rendering and animating of a view’s content.Every view in UIKit is backed by a layer object (usually an instance of theCALayerclass), which manages the backing store for the view and handles view-related animations.Mostoperations you perform should be through theUIViewinterface. However, insituations where you need more control over the rendering or animation behaviorof your view, you can perform operations through its layer instead.
View Hierarchies and Subview Management
Each superview stores its subviews in an ordered array and the order in that array also affects the visibility of each subview.
The arrangement of views in a view hierarchy also determines how your application responds to events. When a touch occurs inside a specific view, the system sends an event object with the touch information directly to that view for handling. However, if the view does not handle a particular touch event, it can pass the event object along to its superview. If the superview does not handle the event, it passes the event object to its superview, and so on up the responder chain. Specific views can also pass the event object to an intervening responder object, such as a view controller. If no object handles the event, it eventually reaches the application object, which generally discards it.
The View Drawing Cycle
The UIView class uses an on-demand drawing model for presenting content. When a view first appears on the screen, the system asks it to draw its content. The system captures a snapshot of this content and uses that snapshot as the view’s visual representation. If you never change the view’s content, the view’s drawing code may never be called again. The snapshot image is reused for most operations involving the view. If you do change the content, you notify the system that the view has changed. The view then repeats the process of drawing the view and capturing a snapshot of the new results.
When the contents of your view change, you do not redraw those changes directly. Instead, you invalidate the view using either the setNeedsDisplay or setNeedsDisplayInRect: method. These methods tell the system that the contents of the view changed and need to be redrawn at the next opportunity. The system waits until the end of the current run loop before initiating any drawing operations. This delay gives you a chance to invalidate multiple views, add or remove views from your hierarchy, hide views, resize views, and reposition views all at once. All of the changes you make are then reflected at the same time.
Changing a view’s geometry does not automatically cause the system to redraw the view’s content. The view’s contentMode property determines how changes to the view’s geometry are interpreted. Most content modes stretch or reposition the existing snapshot within the view’s boundaries and do not create a new one. For more information about how content modes affect the drawing cycle of your view
When the time comes to render your view’s content, the actual drawing process varies depending on the view and its configuration. System views typically implement private drawing methods to render their content. Those same system views often expose interfaces that you can use to configure the view’s actual appearance. For custom UIView subclasses, you typically override the drawRect: method of your view and use that method to draw your view’s content. There are also other ways to provide a view’s content, such as setting the contents of the underlying layer directly, but overriding the drawRect: method is the most common technique.
To understand the relationship between views and layers, ithelps to look at an example.Figure 1-1showsthe view architecture from the ViewTransitions sample application along withthe relationship to the underlying Core Animation layers. The views in theapplication include a window (which is also a view), a genericUIViewobject that acts as acontainer view, an image view, a toolbar for displaying controls, and a barbutton item (which is not a view itself but which manages a view internally).(The actual ViewTransitions sample application includes anadditional image view that is used to implement transitions. For simplicity,and because that view is usually hidden, it is not included inFigure 1-1.) Every view has a corresponding layerobject that can be accessed from that view’s layer property.(Because a bar button item is not a view, you cannot access its layerdirectly.) Behind those layer objects are Core Animation rendering objects andultimately the hardware buffers used to manage the actual bits on the screen.
Each view has a content mode that controls how the view recycles its content in response to changes in the view’s geometry and whether it recycles its content at all. When a view is first displayed, it renders its content as usual and the results are captured in an underlying bitmap. After that, changes to the view’s geometry do not always cause the bitmap to be recreated. Instead, the value in the contentMode property determines whether the bitmap should be scaled to fit the new bounds or simply pinned to one corner or edge of the view.
The content mode of a view is applied whenever you do the following:
- Change the width or height of the view’s frame or bounds rectangles.
- Assign a transform that includes a scaling factor to the view’s transform property.
By default, the contentMode property for most views is set to UIViewContentModeScaleToFill
you can also set the content mode to the UIViewContentModeRedraw value when you specifically want your custom views to redraw themselves during scaling and resizing operations. Setting your view’s content mode to this value forces the system to call your view’s drawRect: method in response to geometry changes. In general, you should avoid using this value whenever possible, and you should certainly not use it with the standard system views
为了通晓视图和层之间的关系，看个例子更有帮助。图1-1体现了ViewTransitions演示应用中的视图布局，以致跟底层的基石动漫层之间的关联。应用中的视图包涵三个窗口，叁个通用的UIView对象，它充当容器视图(container view卡塔尔,一个图像视图，用于显示控件的工具栏，和三个工具栏按键项(它本人不是三个视图，不过在内处了多少个视图卡塔尔。(实际的ViewTransitions演示应用还隐含叁个额外的图像视图，用来落实联网（transitions）。因为非常视图平日是藏身的，为简易起见没包蕴在图1-1里卡塔尔(قطر每二个视图都有一个对应的层对象，能够从该视图的layer属性访谈。(因为工具栏按键项不是三个视图，你不可能间接待上访谈它的层State of Qatar那些层对象的背后是内核动漫渲染对象，最后硬件缓存（hardware buffers）用来管理显示器上的莫过于位。
You specify the stretchable area of a view using the contentStretch
The view’s content mode also plays a role in determining how the view’s stretchable area is used.Stretchable areas are only used when the content mode would cause the view’s content to be scaled. This means that stretchable views are supported only with the UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, and UIViewContentModeScaleAspectFill content modes. If you specify a content mode that pins the content to an edge or corner (and thus does not actually scale the content), the view ignores the stretchable area.
Note: The use of the contentStretch property is recommended over the creation of a stretchable UIImage object when specifying the background for a view. Stretchable views are handled entirely in the Core Animation layer, which typically offers better performance.
Figure 1-1Architecture of the views in a sample application
Built-In Animation Support
frame—Use this to animate position and size changes for the view.
bounds—Use this to animate changes to the size of the view.
center—Use this to animate the position of the view.
transform—Use this to rotate or scale the view.
alpha—Use this to change the transparency of the view.
backgroundColor—Use this to change the background color of the view.
contentStretch—Use this to change how the view’s contents stretch.
View Geometry and Coordinate Systems
The default coordinate system in UIKit has its origin in the top-left corner and has axes that extend down and to the right from the origin point.
Important: Some iOS technologies define default coordinate systems whose origin point and orientation differ from those used by UIKit. For example, Core Graphics and OpenGL ES use a coordinate system whose origin lies in the lower-left corner of the view or window and whose y-axis points upward relative to the screen. Your code must take such differences into account when drawing or creating content and adjust coordinate values (or the default orientation of the coordinate system) as needed.
The use of Core Animation layer objects has importantimplications for performance. The actual drawing code of a view object iscalled as little as possible, and when the code is called, the results arecached by Core Animation and reused as much as possible later. Reusingalready-rendered content eliminates the expensive drawing cycle usually neededto update views.Reuse of this content is especially important during animations, where the existing content can be manipulated.Suchreuse is much less expensive than creating new content.
The Relationship of the Frame, Bounds, and Center Properties
- The frame property contains the frame rectangle, which specifies the size and location of the view in its superview’s coordinate system.
- The bounds property contains the bounds rectangle, which specifies the size of the view (and its content origin) in the view’s own local coordinate system.
- The center property contains the known center point of the view in the superview’s coordinate system.
If you are changing only the position of the view (and not its size), the center property is the preferred way to do so. The value in the center property is always valid, even if scaling or rotation factors have been added to the view’s transform. The same is not true for the value in the frame property, which is considered invalid if the view’s transform is not equal to the identity transform.
By default, a view’s frame is not clipped to its superview’s frame. Thus, any subviews that lie outside of their superview’s frame are rendered in their entirety. You can change this behavior, though, by setting the superview’s clipsToBounds property to YES. Regardless of whether or not subviews are clipped visually, touch events always respect the bounds rectangle of the target view’s superview. In other words, touch events occurring in a part of a view that lies outside of its superview’s bounds rectangle are not delivered to that view.
Coordinate System Transformations
- To modify your entire view, modify the affine transform in the transform property of your view.
- To modify specific pieces of content in your view’s drawRect: method, modify the affine transform associated with the active graphics context.
You would not use this property to make permanent changes to your view,
such as modifying its position or size a view within its superview’s
coordinate space. For that type of change, you should modify the frame
rectangle of your view instead.
Note: When modifying the transform property of your view, all transformations are performed relative to the center point of the view.
In your view’s drawRect: method, you use affine transforms to position and orient the items you plan to draw. Rather than fix the position of an object at some location in your view, it is simpler to create each object relative to a fixed point, typically (0, 0), and use a transform to position the object immediately prior to drawing. That way, if the position of the object changes in your view, all you have to do is modify the transform, which is much faster and less expensive than recreating the object at its new location. You can retrieve the affine transform associated with a graphics context using the CGContextGetCTM function and you can use the related Core Graphics functions to set or modify this transform during drawing.（i don't quiet understand)
Important: If a view’s transform property is not the identity transform, the value of that view’s frame property is undefined and must be ignored. When applying transforms to a view, you must use the view’s bounds and center properties to get the size and position of the view. The frame rectangles of any subviews are still valid because they are relative to the view’s bounds.
In addition to providing its own content, a view can act asa container for other views. When one view contains another, a parent-childrelationship is created between the two views. The child view in therelationship is known as the subview and the parent view is known asthe superview. The creation of this type of relationship has implicationsfor both the visual appearance of your application and the application’s behavior.
Points Versus Pixels
One point does not necessarily correspond to one pixel on the screen.
At the device level, all coordinates you specify in your view must be converted to pixels at some point. However, the mapping of points in the user coordinate space to pixels in the device coordinate space is normally handled by the system. Both UIKit and Core Graphics use a primarily vector-based drawing model where all coordinate values are specified using points. Thus, if you draw a curve using Core Graphics, you specify the curve using the same values, regardless of the resolution of the underlying screen.
The Runtime Interaction Model for Views
Any time a user interacts with your user interface, or any time your own code programmatically changes something, a complex sequence of events takes place inside of UIKit to handle that interaction. At specific points during that sequence, UIKit calls out to your view classes and gives them a chance to respond on behalf of your application.
UIKit interactions with your view objects
The following steps break the event sequence in Figure 1-7 down even further and explain what happens at each stage and how you might want your application to react in response.
- The user touches the screen.
- The hardware reports the touch event to the UIKit framework.
- The UIKit framework packages the touch into a UIEvent object and dispatches it to the appropriate view. (For a detailed explanation of how UIKit delivers events to your views, see Event Handling Guide for iOS.)
- The event-handling code of your view responds to the event. For example, your code might:
- Change the properties (frame, bounds, alpha, and so on) of the view or its subviews.
- Call the setNeedsLayout method to mark the view (or its subviews) as needing a layout update.
- Call the setNeedsDisplay or setNeedsDisplayInRect: method to mark the view (or its subviews) as needing to be redrawn.
- Notify a controller about changes to some piece of data.
Of course, it is up to you to decide which of these things the view should do and which methods it should call.
- If the geometry of a view changed for any reason, UIKit updates its
subviews according to the following rules:
a. If you have configured autoresizing rules for your views, UIKit adjusts each view according to those rules. For more information about how autoresizing rules work, see Handling Layout Changes Automatically Using Autoresizing Rules.
b. If the view implements the layoutSubviews method, UIKit calls it.You can override this method in your custom views and use it to adjust the position and size of any subviews. For example, a view that provides a large scrollable area would need to use several subviews as “tiles” rather than create one large view, which is not likely to fit in memory anyway. In its implementation of this method, the view would hide any subviews that are now offscreen or reposition them and use them to draw newly exposed content. As part of this process, the view’s layout code can also invalidate any views that need to be redrawn.
- If any part of any view was marked as needing to be redrawn, UIKit asks the view to redraw itself.For custom views that explicitly define a drawRect: method, UIKit calls that method. Your implementation of this method should redraw the specified area of the view as quickly as possible and nothing else. Do not make additional layout changes at this point and do not make other changes to your application’s data model. The purpose of this method is to update the visual content of your view.Standard system views typically do not implement a drawRect: method but instead manage their drawing at this time.
- Any updated views are composited with the rest of the application’s visible content and sent to the graphics hardware for display.
- The graphics hardware transfers the rendered content to the screen.
Note: The preceding update model applies primarily to applications
that use standard system views and drawing techniques. Applications that
use OpenGL ES for drawing typically configure a single full-screen view
and draw directly to the associated OpenGL ES graphics context. In such
a case, the view may still handle touch events but, because it is
screen, it would not need to lay out subviews.
Visually, the content of a subview obscures all or part ofthe content of its parent view. If the subview is totally opaque, then the areaoccupied by the subview completely obscures the corresponding area of theparent. If the subview is partially transparent, the content from the two viewsis blended together prior to being displayed on the screen. Each superviewstores its subviews in an ordered array and the order in that array alsoaffects the visibility of each subview. If two sibling subviews overlap eachother, the one that was added last (or was moved to the end of the subviewarray) appears on top of the other.
Tips for Using Views Effectively
- Views Do Not Always Have a Corresponding View Controller
- Minimize Custom Drawing
- Take Advantage of Content Modes
- Declare Views as Opaque Whenever Possible
- Adjust Your View’s Drawing Behavior When Scrolling
- Do Not Customize Controls by Embedding Subviews
The superview-subview relationship also impacts severalview behaviors. Changing the size of a parent view has a ripple effect that cancause the size and position of any subviews to change too. When you change thesize of a parent view, you can control the resizing behavior of each subview byconfiguring the view appropriately. Other changes that affect subviews includehiding a superview, changing a superview’s alpha (transparency), or applying amathematical transform to a superview’s coordinate system.
The arrangement of views in a view hierarchy alsodetermines how your application responds to events. When a touch occurs insidea specific view, the system sends an event object with the touch informationdirectly to that view for handling. However, if the view does not handle aparticular touch event, it can pass the event object along to its superview. Ifthe superview does not handle the event, it passes the event object to itssuperview, and so on up the responder chain. Specific views can also pass theevent object to an interveningresponder object,such as a view controller. If no object handles the event, it eventuallyreaches the application object, which generally discards it.
For more information about how to create view hierarchies,see“Creating and Managing a View Hierarchy.”
至于如何创立视图等级次序的越多音信，请看“Creating and Managing a View Hierarchy.”
TheUIViewclassuses an on-demand drawing model for presenting content. When a view firstappears on the screen, the system asks it to draw its content. The systemcaptures a snapshot of this content and uses that snapshot as the view’s visualrepresentation. If you never change the view’s content, the view’s drawing codemay never be called again. The snapshot image is reused for most operationsinvolving the view. If you do change the content, you notify the system thatthe view has changed. The view then repeats the process of drawing the view andcapturing a snapshot of the new results.
UIView类使用一种按需绘制模型(on-demand drawing modelState of Qatar来表现视图内容。当视图第三次出以往显示屏上时，系统必要它绘制其故事情节。系统捕捉该内容的快速照相，并利用该快速照相作为视图的视觉彰显(visual representationState of Qatar。借使您或多或少也未有改正视图的剧情，那视图的绘图代码大概不会再被调用。快速照相图片被波及视图的大多数操作所接收。借使您对剧情做了更动，你须要文告系统告知它视图已经改造。然后视图重复绘制视图进度，并捕捉新的快速照相。
When the contents of your view change, you do not redrawthose changes directly. Instead, you invalidate the view using either the setNeedsDisplay or setNeedsDisplayInRect:method.These methods tell the system that the contents of the view changed and need tobe redrawn at the next opportunity. The system waits until the end of thecurrent run loop before initiating any drawing operations. This delay gives youa chance to invalidate multiple views, add or remove views from your hierarchy,hide views, resize views, and reposition views all at once. All of the changesyou make are then reflected at the same time.
Note: Changinga view’s geometry does not automatically cause the system to redraw the view’scontent. The view’scontentModepropertydetermines how changes to the view’s geometry are interpreted. Most contentmodes stretch or reposition the existing snapshot within the view’s boundariesand do not create a new one. For more information about how content modesaffect the drawing cycle of your view, see“Content Modes.”
When the time comes to render your view’s content, the actual drawing process varies depending on the view and its configuration. System views typically implement private drawing methods to render their content. Those same system views often expose interfaces that you can use to configure the view’s actual appearance. For customUIViewsubclasses, you typicallyoverride the drawRect:method of your view and use that method to draw yourview’s content. There are also other ways to provide a view’s content, such assetting the contents of the underlying layer directly, but overriding thedrawRect:method is the mostcommon technique.
For more information about how to draw content for customviews, see“Implementing Your Drawing Code.”
越来越多关于怎么样绘制自定义视图的内容方面包车型大巴音讯，请看“Implementing Your Drawing Code.”
Each view has a content mode that controls how the viewrecycles its content in response to changes in the view’s geometry and whetherit recycles its content at all. When a view is first displayed, it renders itscontent as usual and the results are captured in an underlying bitmap. Afterthat, changes to the view’s geometry do not always cause the bitmap to berecreated. Instead, the value in thecontentModepropertydetermines whether the bitmap should be scaled to fit the new bounds or simplypinned to one corner or edge of the view.
The content mode of a view is applied whenever you do thefollowing:
·Change the width or height ofthe view’sframeorboundsrectangles.
·Assign a transform thatincludes a scaling factor to the view’stransformproperty.
By default, thecontentModeproperty for most views is set to UIViewContentModeScaleToFill,which causes the view’s contents to be scaled to fit the new frame size.Figure 1-2shows the results that occur for somecontent modes that are available. As you can see from the figure, not allcontent modes result in the view’s bounds being filled entirely, and those thatdo might distort the view’s content.
Figure 1-2 Content mode comparisons