In iOS, you use windows and views to present your application’s content on the screen. Windows do not have any visible content themselves but provide a basic container for your application’s views. Views define a portion of a window that you want to fill with some content. For example, you might have views that display images, text, shapes, or some combination thereof. You can also use views to organize and manage other views.
在iOS中,人们使用窗体(windows)和视图(views)来向使用者呈现应用界面。
窗体(windows)本身并不拥有任何可视内容,它仅作为应用中视图(views) 的容器。
窗体中各部分的显示内容在视图(views)中定义。例如,你可以利用视图来显示图片、文本、图形或前者的各种组合内容。另外,还可以利用某个视图来组织和管理其他视图。
Every application has at least one window and one view for presenting its content. UIKit and other system frameworks provide predefined views that you can use to present your content. These views range from simple buttons and text labels to more complex views such as table views, picker views, and scroll views. In places where the predefined views do not provide what you need, you can also define custom views and manage the drawing and event handling yourself.
一个应用中至少存在一个窗体,以及一个用于显示应用界面的视图。
UIKit和其他系统框架提供了许多可供使用的视图类,包括基本的按钮(button)、标签(text label),以及其他复杂的视图,如table view,picker view,scroll view等等。
当框架内的视图不能满足需求时,还可以通过继承原有视图类来自定义视图,并在自定义视图中管理界面的绘制或处理事件等。
作为界面管理者的view:
A view is an instance of the UIView class (or one of its subclasses) and manages a rectangular area in your application window. Views are responsible for drawing content, handling multitouch events, and managing the layout of any subviews. 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. A view responds to touch events in its rectangular area either by using gesture recognizers or by handling touch events directly. In the view hierarchy, parent views are responsible for positioning and sizing their child views and can do so dynamically. This ability to modify child views dynamically lets your views adjust to changing conditions, such as interface rotations and animations.
通过UIView类(或它的子类)来创建视图对象。
视图对象在窗体上管理着一个矩形区域,并且处理在这个区域内发生的诸如图形显示、触控和子视图布局等操作。
视图显示操作涉及到许多技术,如Core Graphics,OpenGL ES等,另外还使用到UIKit框架来进行图形图像或文本的绘制输出。
You can think of views as building blocks that you use to construct your user interface. Rather than use one view to present all of your content, you often use several views to build a view hierarchy. Each view in the hierarchy presents a particular portion of your user interface and is generally optimized for a specific type of content. For example, UIKit has views specifically for presenting images, text and other types of content.
可以将视图理解为界面的基本构建单元。通常应用界面由若干视图构成的视图树组成,而非仅仅使用一个视图。视图树中的每一个视图都关联到界面的特定部分,并且通常都针对特定用途进行了优化。例如,UIKit中有专门用于图片、文本或其他内容的视图。
Relevant Chapters: View and Window Architecture, Views
通过窗体来协调各个视图进行显示
A window is an instance of the UIWindow class and handles the overall presentation of your application’s user interface. Windows work with views (and their owning view controllers) to manage interactions with, and changes to, the visible view hierarchy. For the most part, your application’s window never changes. After the window is created, it stays the same and only the views displayed by it change. Every application has at least one window that displays the application’s user interface on a device’s main screen. If an external display is connected to the device, applications can create a second window to present content on that screen as well.
使用UIWindow类可以创建窗体对象,窗体对象的作用是统筹协调应用界面元素,即视图。窗体同视图(以及它们对应的视图控制器)一起,管理着外界对视图树的交互和修改。大多数情况下,应用中的窗体不会改变。一旦窗体创建完毕后,它便一直保持原样,只有它里面的视图才会被改变。应用至少会用到一个窗体来显示界面到主屏幕(main screen)上。如果在设备上连接有外部显示器,则应用也可以再创建另外一个窗体用于外部显示器上的显示。
Relevant Chapters: Windows
界面改变可以通过动画进行反馈
Animations provide users with visible feedback about changes to your view hierarchy. The system defines standard animations for presenting modal views and transitioning between different groups of views. However, many attributes of a view can also be animated directly. For example, through animation you can change the transparency of a view, its position on the screen, its size, its background color, or other attributes. And if you work directly with the view’s underlying Core Animation layer object, you can perform many other animations as well.
使用动画可以进行动态反馈(针对视图树的改变)。系统定义了一些用于视图显现方式和视图切换的标准动画。另外,视图的许多属性也能直接被动画显示。比如视图透明度、位置、尺寸、背景色或其他属性的改变都可以拥有动画效果。
当你直接使用视图下的核心动画层(Core Animation layer)对象时,还可以操纵更多的动画效果。
Relevant Chapters: Animations
Interface Builder的作用
Interface Builder is an application that you use to graphically construct and configure your application’s windows and views. Using Interface Builder, you assemble your views and place them in a nib file, which is a resource file that stores a freeze-dried version of your views and other objects. When you load a nib file at runtime, the objects inside it are reconstituted into actual objects that your code can then manipulate programmatically.
利用IB可以对应用的窗体和视图进行可视化设计。
首先在nib文件(一种资源文件,里面包含着视图和其他对象的静态映像)中组装好视图,然后在运行时加载nib文件,文件内的对象被重构用以显示,这样更易于在代码中对显示对象进行控制。
Interface Builder greatly simplifies the work you have to do in creating your application’s user interface. Because support for Interface Builder and nib files is incorporated throughout iOS, little effort is required to incorporate nib files into your application’s design.
使用IB可以极大地简化界面设计工作,因为对IB和nib文件的支持贯穿iOS平台,只需要极小的代价即可将nib文件加入到你的应用设计中来。
For more information about how to use Interface Builder, see Interface Builder User Guide. For information about how view controllers manage the nib files containing their views,see Creating Custom Content View Controllers in View Controller Programming Guide for iOS.
关于Interface Builder的更多内容请参阅Interface Builder User Guide。更多关于视图控制器对相应nib文件的管理方式,详见View Controller Programming Guide中Creating Custom Content View Controllers章节。
后记
Because views are very sophisticated and flexible objects, it would be impossible to cover all of their behaviors in one document. However, other documents are available to help you learn about other aspects of managing views and your user interface as a whole.
为帮助读者更全面地了解视图管理及其他相关内容,这里提供额外的阅读引导:
View controllers are an important part of managing your application’s views. A view controller presides over all of the views in a single view hierarchy and facilitates the presentation of those views on the screen. For more information about view controllers and the role they play, see View Controller Programming Guide for iOS.
一、关于视图控制器的更多内容
View Controller Programming Guide for iOS
Views are the key recipients of gesture and touch events in your application. For more information about using gesture recognizers and handling touch events directly, see Event Handling Guide for iOS.
二、更多关于手势控制和触摸事件处理的内容:
Event Handling Guide for iOS
Custom views must use the available drawing technologies to render their content. For information about using these technologies to draw within your views, see Drawing and Printing Guide for iOS.
三、更多关于自定义视图的绘制和渲染的内容:
Drawing and Printing Guide for iOS
In places where the standard view animations are not sufficient, you can use Core Animation. For information about implementing animations using Core Animation, see Core Animation Programming Guide.
四、更多关于Core Animation动画(可以实现更高性能动画)实现的内容:
Core Animation Programming Guide
窗体和视图的构成
Views and windows present your application’s user interface and handle the interactions with that interface. UIKit and other system frameworks provide a number of views that you can use as-is with little or no modification. You can also define custom views for places where you need to present content differently than the standard views allow.
窗体和视图中可以呈现应用界面,并处理界面内的交互行为。UIKit
Whether you use the system views or create your own custom views, you need to understand the infrastructure provided by the UIView and UIWindow classes. These classes provide sophisticated facilities for managing the layout and presentation of views. Understanding how those facilities work is important for making sure your views behave appropriately when changes occur in your application.
Most of the things you might want to do visually are done with view objects—instances of the UIView class. A view object defines a rectangular region on the screen and handles the drawing and touch events in that region. A view can also act as a parent for other views and coordinate the placement and sizing of those views. The UIView class does most of the work in managing these relationships between views, but you can also customize the default behavior as needed.
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.
To understand the relationship between views and layers, it helps to look at an example. Figure 1-1 shows the view architecture from the ViewTransitions sample application along with the relationship to the underlying Core Animation layers. The views in the application include a window (which is also a view), a generic UIView object that acts as a container view, an image view, a toolbar for displaying controls, and a bar button item (which is not a view itself but which manages a view internally). (The actual ViewTransitions sample application includes an additional image view that is used to implement transitions. For simplicity, and because that view is usually hidden, it is not included in Figure 1-1.) 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.