52ky 发表于 2022-5-2 12:29:19

IOS 开发的窗口和视图

视图是应用程序的界面。视图可以使用 NIB 文件实现或使用代码创建。视图也是响应者(UIRESPONDER 的子类),这意味着视图可以与用户交互。因此,视图不仅是用户可以看到的界面,也是用户可以与之交互的界面。


视图相关结构的名称、属性和功能
CGPoint {x,y} 坐标信息 视图所在的坐标信息
CGSize {width,height} 视图所在的宽高尺寸信息
CGRect {origin,size} 视图所在的CGPoint和CGSize的组合坐标(视图左上角的点)和大小信息。
查看相关结构函数
CGPointMake(x,y) 声明位置信息
CGSizeMake(width,height) 声明尺寸信息
CGRectMake(x,y,width,height) 声明位置和大小写信息
框架和边界
视图的位置和大小可以用两种方式表示。一种方式是Frame(框架),即以它的父视图为起点,获取自己的位置信息。另一种方式是Bound,即以自身为起点推导出其位置。

frame 和 bounds 是 UIView 中的两个属性。
框架是指:视图在父视图坐标系中的位置和大小。 (参考点是父坐标系)
Bounds是指:视图在自己的坐标系中的位置和大小。 (参考点是自己的坐标系)

- ( CGRect ) frame {
   return CGRectMake ( self . frame . origin . x , self . frame . origin . y , self . frame . size . width , self . frame . size . height ) ;
}
- ( CGRect ) bounds {
   return CGRectMake ( 0 , 0 , self . frame . size . width , self . frame . size . height ) ;
}

下图中,View B 是 View A 的子视图,那么 View B 的 frame 属性是 origin(200, 100),size (200, 250),View B 的 bounds 属性是 origin (0, 0 ), 大小 (200, 250)。

ios-框架-框架

center 属性使用 CGPoint 来表示矩形的中心点在其父视图中的位置。 如上图所示,View B的center属性为(300, 200)。

frame、bounds、center这三个属性相互关联,相互影响。 当其中一个属性发生变化时,其他属性也会发生变化。

屏幕、窗口和视图创建可视化界面
A UIScreen object that identifies a physical screen connected to the device.
A UIWindow object that provides drawing support for the screen.
A set of UIView objects to perform the drawing. These objects are attached to the window and draw their contents when the window asks them to.

用户界面
UIScreen 对象表示 iOS 设备的物理屏幕()。 此屏幕对象在视图布局边界和像素空间之间建立映射。 它返回全屏大小(边界)或仅返回应用程序占用的矩形(applicationFrame)。

CGrect screenBounds = [ [ UIScreen mainScreen ] bounds ] ; //返回的是带有状态栏的Rect
对于 iphone4来说 screenBounds = 0, 0, 320, 480

CGRect viewBounds = [ [ UIScreen mainScreen ] applicationFrame ] ; //不包含状态栏的Rect
对于 iphone4来说, viewBounds = 0, 20, 320, 460

界面窗口
视图层次结构的最顶层是应用程序窗口,它是 UIWindow 的一个实例,它是 UIView 的子类。窗口的主要作用是提供一个区域来显示视图(容器),二是向视图分发事件。窗口本身没有任何可见的内容,但它为应用程序的视图提供了一个基本窗口。应用程序应该只有一个主窗口,占据整个屏幕构成背景,是所有可见视图的父视图,其他视图是它的子视图。

窗口是项目主 nib 文件中的顶级 nib 对象。当应用程序运行时,系统会自动加载nib文件并实例化窗口。主 nib 文件中的另一个顶级 nib 对象是应用程序委托,它有一个指向窗口的窗口出口。当程序运行时,系统会生成一个应用程序委托的实例。它的 window 属性指向窗口。

通常我们可以采取两种方法给 UIWindow 添加视图:

1.添加子视图

通过 addSubview 方法直接将视图添加到窗口中。程序负责维护视图的生命周期和刷新,但并不关心视图对应的ViewController。因此,以这种方式将视图添加到窗口后,我们必须保留视图。对应ViewController的有效期不能过早释放。

2.rootViewController

rootViewController 是 UIWindow 的一种遍历方法。通过将该属性设置为要添加的视图对应的ViewController,UIWindow会自动将其视图添加到当前窗口,并负责维护ViewController和视图的生命周期,防止过早释放。

UIWindow在显示的时候会按照UIWindowLevel进行排序,也就是等级高的会排在比他低的所有等级的前面。我们来看看UIWindowLevel的定义:

typedef CGFloat UIWindowLevel ;

UIKIT_EXTERN const UIWindowLevel UIWindowLevelNormal ;
UIKIT_EXTERN const UIWindowLevel UIWindowLevelAlert ;
UIKIT_EXTERN const UIWindowLevel UIWindowLevelStatusBar ;
iOS系统定义了三个窗口层级,每个层级又可以分为很多子层级(可以从UIWindow头文件中看到成员变量CGFloat_windowSublevel;),但是系统并没有打开该属性。 UIWindow 的默认级别是 UIWindowLevelNormal。 UIWindowLevelNormal<UIWindowLevelStatusBar<UIWindowLevelAlert.

界面视图
视图是 UIView 类的一个实例,负责在屏幕上定义一个矩形区域。在 iPhone 应用程序中,视图在呈现用户界面和响应用户界面交互方面起着关键作用。每个视图对象负责渲染视图矩形中的内容并响应该区域中的触摸事件。这种双重行为意味着视图是应用程序与用户交互的重要机制。在基于模型-视图-控制器的应用程序中,视图对象显然属于视图部分。

UIView 有一个 superview 属性和一个 subviews 属性。 subviews 是 UIViews 的 NSArray,按从后到前的顺序放置。这有助于在代码中访问视图层次结构。 isDescendantOfView 方法可以判断一个视图是否是另一个视图的子视图。

UIView 提供了许多创建和管理视图的方法。

1.添加视图

   insertSubview:atIndex: //放置子视图数组的具体索引位置
   insertSubview:aboveSubview: //在一个子视图之前
   insertSubview:aboveSubview: //在一个子视图之前
2.重新排序和删除子视图

      //交换两个view的位置
      bringSubviewToFront: 和 sendSubviewToBack:// 会提前激活子视图
      //移除一个视图的子视图
3.查看回调

一旦视图的层次结构发生变化,视图就会收到回调。

   一种。调用 addSubivew: 成功后,会向视图发送一个 didAddSubivew: 回调,在添加视图时触发 UIView 的子类执行其他操作。
   湾。 didMoveToSuperview:将通知相关视图其父视图已更改。
   C。 WillMoveToSuperview:在视图移动之前发出回调
   d。 didMoveToWindow:回调与didMoveToSuperview:类似,从命名上可以看出区别。
   e. willMoveToWindow:在视图移动之前发出的回调。
    F。 willRemoveToSubview:回调通知父视图子视图即将被移除


UIView 处理直接屏幕绘制。它的 drawRect: 方法提供了一种直接绘制内容的低级方法,允许使用 Quartz 2D 调用来创建和显示任意元素,这些元素可以组合起来构建具体的、可操作的界面。

当用户触摸屏幕时,Touchview 类会收集一系列点,并且在每次触摸移动时,touchesMoved:WithEvent: 方法都会调用 setNeedsDisplay。这反过来会触发对 drawRect: 方法的调用,其中视图将点绘制为线段以创建可见的屏幕路径。

(A view is the interface of an application. Views can be implemented using NIB files or created using code. Views are also responders (subclasses of uiresponder), which means that views can interact with users. Therefore, a view is not only an interface that users can see, but also an interface with which users can interact.
Names, properties, and functions of view related structures
Coordinate information of cgpoint {x, y} coordinate information view
Cgsize {width, height} the width and height dimension information of the view
The combined coordinates (the point in the upper left corner of the view) and size information of the cgpoint and cgsize where the cgrect {origin, size} view is located.
View related structure functions
Cgpointmake (x, y) declares location information
Cgsizemake (width, height) declares size information
Cgrectmake (x, y, width, height) declares location and case information
Frame and boundary
The position and size of the view can be represented in two ways. One way is frame, which takes its parent view as the starting point to obtain its own location information. Another way is bound, that is to deduce its position from itself.
Frame and boundaries are two attributes in uiview.
Frame refers to the position and size of the view in the parent view coordinate system. (the reference point is the parent coordinate system)
Bounds refers to the position and size of the view in its own coordinate system. (the reference point is its own coordinate system)
- ( CGRect ) frame {
return CGRectMake ( self . frame . origin . x , self . frame . origin . y , self . frame . size . width , self . frame . size . height ) ;
}
- ( CGRect ) bounds {
return CGRectMake ( 0 , 0 , self . frame . size . width , self . frame . size . height ) ;
}
In the following figure, View B is a sub view of view a, so the frame attribute of view B is origin (200, 100), size (200, 250), and the boundaries attribute of view B is origin (0, 0), size (200, 250).
IOS framework
The center property uses cgpoint to represent the position of the center point of the rectangle in its parent view. As shown in the above figure, the center attribute of view B is (300, 200).
Frame, boundaries and center are interrelated and affect each other. When one of the attributes changes, the other attributes also change.
Create visual interfaces for screens, windows, and views
A UIScreen object that identifies a physical screen connected to the device.
A UIWindow object that provides drawing support for the screen.
A set of UIView objects to perform the drawing.These objects are attached to the window and draw their contents when the window asks them to.
user interface
The uiscreen object represents the physical screen of the IOS device (). This screen object establishes a mapping between the view layout boundary and the pixel space. It returns the full screen size (boundary) or only the application frame occupied by the application.
CGrect screenBounds = [ [ UIScreen mainScreen ] bounds ] ; // Rect with status bar is returned
For iPhone 4, screenbounds = 0, 0320480
CGRect viewBounds = [ [ UIScreen mainScreen ] applicationFrame ] ; // Rect without status bar
For iPhone 4, viewboundaries = 0, 20320460
Interface window
The top layer of the view hierarchy is the application window, which is an instance of UIWindow and a subclass of uiview. The main function of the window is to provide an area to display the view (container), and the other is to distribute events to the view. The window itself has nothing visible, but it provides a basic window for the view of the application. The application should have only one main window, which occupies the whole screen and constitutes the background. It is the parent view of all visible views, and other views are its child views.
Windows are top-level NIB objects in the project's main NIB file. When the application runs, the system will automatically load the nib file and instantiate the window. Another top-level NIB object in the main NIB file is the application delegate, which has a window exit to the window. When the program runs, the system will generate an instance of the application delegate. Its window property points to the window.
Generally, we can add views to UIWindow in two ways:
1. Add sub view
Add the view directly to the window through the addsubview method. The program is responsible for maintaining the life cycle and refresh of the view, but does not care about the viewcontroller corresponding to the view. Therefore, after adding the view to the window in this way, we must keep the view. The validity period of the corresponding viewcontroller cannot be released prematurely.
2.rootViewController
Rootviewcontroller is a traversal method of UIWindow. By setting this property to the viewcontroller corresponding to the view to be added, UIWindow will automatically add its view to the current window, and is responsible for maintaining the life cycle of viewcontroller and view to prevent premature release.
When UIWindow is displayed, it will be sorted according to uiwindowlevel, that is, the higher level will be in front of all lower levels. Let's look at the definition of uiwindowlevel:
typedef CGFloat UIWindowLevel ;
UIKIT_ EXTERN const UIWindowLevel UIWindowLevelNormal ;
UIKIT_ EXTERN const UIWindowLevel UIWindowLevelAlert ;
UIKIT_ EXTERN const UIWindowLevel UIWindowLevelStatusBar ;
The IOS system defines three window levels, and each level can be divided into many sub levels (you can see the member variable cgfloat_windowsublevel;) from the UIWindow header file, However, the system does not open this property. The default level of UIWindow is uiwindowlevelnormal. UIWindowLevelNormal<UIWindowLevelStatusBar<UIWindowLevelAlert.
Interface view
View is an instance of uiview class, which is responsible for defining a rectangular area on the screen. In iPhone applications, views play a key role in presenting the user interface and responding to user interface interactions. Each view object is responsible for rendering the contents of the view rectangle and responding to touch events in the area. This dual behavior means that view is an important mechanism for application interaction with users. In model view controller based applications, view objects obviously belong to the view part.
Uiview has a superview attribute and a subviews attribute. Subviews is the nsarray of uiviews, which is placed from back to front. This helps to access the view hierarchy in your code. The isdecendentofview method can determine whether a view is a child view of another view.
Uiview provides many ways to create and manage views.
1. Add view
Insertsubview: atindex: / / the specific index position where the subview array is placed
Insertsubview: aboutsubview: / / before a subview
Insertsubview: aboutsubview: / / before a subview
2. Reorder and delete subviews
/ / exchange the location of two views
Bringsubviewtofront: and sendsubviewtoback: / / activate subviews in advance
/ / remove a subview of a view
3. View callback
Once the hierarchy of the view changes, the view will receive a callback.
One. After calling addsubivew: successfully, a didaddsubivew: callback will be sent to the view to trigger the subclass of uiview to perform other operations when adding the view.
Bay. Didmovetosuperview: the related view will be notified that its parent view has changed.
C。Willmovetosuperview: issue a callback before the view is moved
d。Didmovetowindow: callback is similar to didmovetosuperview: the difference can be seen from the naming.
e. Willmovetowindow: callback issued before the view is moved.
F。Willremovetosubview: the callback notifies the parent view that the child view is about to be removed
Uiview handles direct screen rendering. Its drawRect: method provides a low-level method for directly drawing content, allowing the use of quartz 2D calls to create and display arbitrary elements, which can be combined to build a specific and operable interface.
When the user touches the screen, the touchview class collects a series of points, and the touchesmoved: withevent: method calls setneedsdisplay every time the user touches the move. This in turn triggers a call to the drawRect: method, where the view draws points as line segments to create a visible screen path.
)



页: [1]
查看完整版本: IOS 开发的窗口和视图