Class
UIView
--An object that manages the content for a rectangular area on the screen.
一个对象,用于管理屏幕上矩形内的内容。
Declaration
class UIView : UIResponder //继承于UIResponder
Overview
--Views are the fundamental building blocks of your app's user interface, and the UIView
class defines the behaviors that are common to all views. A view object renders content within its bounds rectangle and handles any interactions with that content. The UIView
class is a concrete class that you can instantiate and use to display a fixed background color. You can also subclass it to draw more sophisticated content. To display labels, images, buttons, and other interface elements commonly found in apps, use the view subclasses provided by the UIKit framework rather than trying to define your own.
一个view渲染的视图内容和客户交互事件,都是在矩形内进行的。非必要时,建议用api提供的view的子类来满足需求。
--Because view objects are the main way your application interacts with the user, they have a number of responsibilities. Here are just a few:
view的职责主要有以下方面:
--Drawing and animation
绘制视图以及动画化
-
Views draw content in their rectangular area using UIKit or Core Graphics.
view 使用 UIKit或Core Graphics技术来在矩形内渲染视图内容
-
Some view properties can be animated to new values.
view有一部分属性是可以动画化到另一个值的。
--Layout and subview management
负责子view的布局和管理
-
Views may contain zero or more subviews.
view可以含有0或多个子view
-
Views can adjust the size and position of their subviews.
view可以调整子view的尺寸和大小
-
Use Auto Layout to define the rules for resizing and repositioning your views in response to changes in the view hierarchy.
--Event handling
事件处理
-
A view is a subclass of
UIResponder
and can respond to touches and other types of events.
view是UIResponder
的子类,可以响应触摸事件和其他事件。
-
Views can install gesture recognizers to handle common gestures.
view可以安装手势识别器,并且可以处理一些公共的手势
--Views can be nested inside other views to create view hierarchies, which offer a convenient way to organize related content. Nesting a view creates a parent-child relationship between the child view being nested (known as the subview) and the parent (known as the superview). A parent view may contain any number of subviews but each subview has only one superview. By default, when a subview’s visible area extends outside of the bounds of its superview, no clipping of the subview's content occurs. Use the clipsToBounds
property to change that behavior.
view可以内嵌进view的视图层次中,父view内嵌了子view,一父多子关系。默认,子view的视图内容不会被裁剪,会超出父view的矩形,你可以设置父view的clipsToBounds
属性来进行裁剪。
--The geometry of each view is defined by its frame
and bounds
properties. The frame
property defines the origin and dimensions of the view in the coordinate system of its superview. The bounds
property defines the internal dimensions of the view as it sees them and is used almost exclusively in custom drawing code. The center property provides a convenient way to reposition a view without changing its frame
or bounds
properties directly.
view的几何形状由frame
and bounds
属性确定。frame属性,定义了在父视图的坐标系中的的原点和维度。bounds属性,定义了view自身的本地坐标系,一般专门用于自定义视图的绘制。center属性,用于重定位view的位置。
--For detailed information about how to use the UIView
class, see View Programming Guide for iOS.
更多使用view的信息,看超链接。
Creating a View -- 创建一个View
--Normally, you create views in your storyboards by dragging them from the library to your canvas. You can also create views programmatically. When creating a view, you typically specify its initial size and position relative to its future superview. For example, the following example creates a view and places its top-left corner at the point (10, 10) in the superview's coordinate system (once it is added to that superview).
初始化view时,要指定在父view中的原点和尺寸,下面是示例代码:
let rect = CGRect(x: 10, y: 10, width: 100, height: 100)
let myView = UIView(frame: rect)
--To add a subview to another view, call the addSubview(_:)
method on the superview. You may add any number of subviews to a view, and sibling views may overlap each other without any issues in iOS. Each call to the addSubview(_:)
method places the new view on top of all other siblings. You can specify the relative z-order of subview by adding it using the insertSubview(_:aboveSubview:)
and insertSubview(_:belowSubview:)
methods. You can also exchange the position of already added subviews using the exchangeSubview(at:withSubviewAt:)
method.
父view的addSubview(_:)
方法可以添加子view;每调用一次addSubview(_:)
方法,就会压在上一个子view的上面,你可以调用子view的insertSubview(_:aboveSubview:)
and insertSubview(_:belowSubview:)
方法来确定子view的z轴顺序,也就是插入子view层,汉堡包那样。然后你可以调用父view的exchangeSubview(at:withSubviewAt:)
方法来交换子view的几何位置。
After creating a view, create Auto Layout rules to govern how the size and position of the view change in response to changes in the rest of the view hierarchy. For more information, see Auto Layout Guide.
创建自动布局规则,适应父view的几何变化。参考超链接。
The View Drawing Cycle -- View的绘制周期
--View drawing occurs on an as-needed basis. When a view is first shown, or when all or part of it becomes visible due to layout changes, the system asks the view to draw its contents. For views that contain custom content using UIKit or Core Graphics, the system calls the view’s draw(_:)
method. Your implementation of this method is responsible for drawing the view’s content into the current graphics context, which is set up by the system automatically prior to calling this method. This creates a static visual representation of your view’s content that can then be displayed on the screen.
view的绘制是需要时才会触发的。当view第一节展示时,或者当view的某部分视图变为可见时,系统就会要求view来绘制它的视图内容。系统通过调用view的draw(_:)
方法来绘制view的视图内容,view的视图内容首先会被绘制进“当前的图形上下文”中,而“当前的图形上下文”则是由系统自动设置的,在view绘制图形之前就已经设置好了。
--When the actual content of your view changes, it is your responsibility to notify the system that your view needs to be redrawn. You do this by calling your view’s setNeedsDisplay()
or setNeedsDisplay(_:)
method of the view. These methods let the system know that it should update the view during the next drawing cycle. Because it waits until the next drawing cycle to update the view, you can call these methods on multiple views to update them at the same time.
当view的实际的内容发生改变时,是由你负责通知系统说你要重新绘制视图内容的。你可以调用view的setNeedsDisplay()
or setNeedsDisplay(_:)
方法来通知系统。通知之后,系统会在下一个绘制周期更新view。
Note
--If you are using OpenGL ES to do your drawing, you should use the GLKView
class instead of subclassing UIView
. For more information about how to draw using OpenGL ES, see OpenGL ES Programming Guide.
关于用OpenGL ES 技术绘制图形,看超链接。这是另外一种技术,我还没用过。
--For detailed information about the view drawing cycle and the role your views have in this cycle, see View Programming Guide for iOS.
关于view的绘制周期,看超链接。
Animations -- 动画
--Changes to several view properties can be animated—that is, changing the property creates an animation starting at the current value and ending at the new value that you specify. The following properties of the UIView
class are animatable:
view的以下几个属性的变化都是可以动画化的。
--To animate your changes, create a UIViewPropertyAnimator
object and use its handler block to change the values of your view's properties. The UIViewPropertyAnimator
class lets you specify the duration and timing of your animations, but it performs the actual animations. You can pause a property-based animator that is currently running to interrupt the animation and drive it interactively. For more information, see UIViewPropertyAnimator
.
你可以创建一个UIViewPropertyAnimator
对象,并使用该对象的“处理代码块”来动画化属性的值的变化。你可以暂停基于属性的动画效果,还可以交互式地动画,具体看UIViewPropertyAnimator
Threading Considerations -- 线程相关
--Manipulations to your application’s user interface must occur on the main thread. Thus, you should always call the methods of the UIView
class from code running in the main thread of your application. The only time this may not be strictly necessary is when creating the view object itself, but all other manipulations should occur on the main thread.
你对view的所有操作都应该在主线程中。
Subclassing Notes -- 子类化建议
--The UIView
class is a key subclassing point for visual content that also requires user interactions. Although there are many good reasons to subclass UIView
, it is recommended that you do so only when the basic UIView
class or the standard system views do not provide the capabilities that you need. Subclassing requires more work on your part to implement the view and to tune its performance.
能用系统提供的view就用系统的,实在不行,再考虑自定义子类化的view。
--For information about ways to avoid subclassing, see Alternatives to Subclassing.
关于更多子类化的内容,看超链接。
Methods to Override -- 复写的方法(或属性)
--When subclassing UIView
, there are only a handful of methods you should override and many methods that you might