View Program iOS

View and Window Architecture

The view draw cycle

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.

当你的view内容变化的时候,我们不直接重绘它,而是通过setNeedsDisplaysetNeedsDisplayInRect:方法通知系统去帮我们完成重绘。

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.

Content Mode

设置当view的形状发生变化时,它的content如何重绘,例如图片是拉伸,平铺等。

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.

一般有这些类型

  • UIViewContentModeScaleAspectFill

  • UIViewContentModeScaleAspectFit

  • UIViewContentModeScaleToFill

你也可以使用自己定义的drawRect:方法来自定义,此时应该设置Contenct Mode为:

  • UIViewContentModeRedraw

Built-In Animation Support

要使用动画,只需两个步骤

  • 告诉UIKit你希望执行动画

  • 改变属性的值

Tell UIKit that you want to perform an animation.
Change the value of the property.

View Geometry and Coordinate Systems

坐标系统

The Relationship of the Frame, Bounds, and Center Properties

A view object tracks its size and location using its 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.

更改view的形状和坐标值,一边我们使用Frame和Center属性。

You use the center and frame properties primarily for manipulating the geometry of the current view.If you are changing only the position of the view (and not its size), the center property is the preferred way to do so.

Bounds就是定义了在view自身中的一个区域, 在这个区域内画的图形才是可见的.

You use the bounds property primarily during drawing. The bounds rectangle is expressed in the view’s own local coordinate system. The default origin of this rectangle is (0, 0) and its size matches the size of the frame rectangle. Anything you draw inside this rectangle is part of the view’s visible content.

Points Versus Pixels

在iOS中的所有坐标值和距离都是用浮点数坐标Points表示的.

Device Screen dimensions (in points)

iPhone and iPod touch devices with 4-inch Retina display 320 x 568

Other iPhone and iPod touch devices 320 x 480

iPad 768 x 1024

Windows

每个iOS程序至少需要一个窗口(UIWindow类), 有些可能程序可能需要多个. 一个window主要负责这些事项:

一个提供显示的区域, 传递事件和与view controller一起为屏幕旋转提供支持.

Every iOS application needs at least one window—an instance of the UIWindow class—and some may include more than one window. A window object has several responsibilities:

  • It contains your application’s visible content.

  • It plays a key role in the delivery of touch events to your views and other application objects.

  • It works with your application’s view controllers to facilitate orientation changes.

In iOS, windows do not have title bars, close boxes, or any other visual adornments. A window is always just

a blank container for one or more views. Also, applications do not change their content by showing new

windows. When you want to change the displayed content, you change the frontmost views of your window

instead.

Creating a Window Programmatically

通过代码创建窗口


self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

Adding Content to Your Window

通常每个window都有一个root view对象, 它包含所有其他views并显示内容. 这样的话如果你需要更新窗口中的内容, 你仅需要替换root view就可以完成. 安装一个view到你的window中, 使用addSubview:方法. 例如安装一个由view controller管理的视图:

Each window typically has a single root view object (managed by a corresponding view controller) that contains

all of the other views representing your content. Using a single root view simplifies the process of changing

your interface; to display new content, all you have to do is replace the root view. To install a view in your

window, use the addSubview: method. For example, to install a view that is managed by a view controller,

you would use code similar to the following:

[window addSubview:viewController.view];

如果在nib设置中设置了root viewController, 当window加载时, UIKit将自动安装root viewController中的view到window中.

If the root view of your window is provided by a container view controller (such as a tabbar controller, navigation controller, or split-view controller), you do not need to set the initial size of the view yourself. The container view controller automatically sizes its view appropriately based on whether the status bar is visible.

Monitoring Window Changes

If you want to track the appearance or disappearance of windows inside your application, you can do so using these window-related notifications:

  • UIWindowDidBecomeVisibleNotification

  • UIWindowDidBecomeHiddenNotification

  • UIWindowDidBecomeKeyNotification

  • UIWindowDidResignKeyNotification

View

由于view是你的程序中与用户最主要的交互的交互方式, view有非常多的功能, 例如下面这些:

  • Layout and subview management

    • A view defines its own default resizing behaviors in relation to its parent view.

    • A view can manage a list of subviews.

    • A view can override the size and position of its subviews as needed.

    • A view can convert points in its coordinate system to the coordinate systems of other views or the

window.

  • Drawing and animation

    • A view draws content in its rectangular area.

    • Some view properties can be animated to new values.

  • Event handling

    • A view can receive touch events.

    • A view participates in the responder chain.

Creating and Configuring View Objects

手动在程序中创建一个view:


CGRect viewRect = CGRectMake(0, 0, 100, 100); UIView* myView = [[UIView alloc] initWithFrame:viewRect];

Adding and Removing Subviews

主要有下面这些方法:

  • addSubview:

  • insertSubview:...

  • bringSubviewToFront:

  • sendSubviewToBack:

  • exchangeSubviewAtIndex:withSubviewAtIndex:

  • removeFromSuperview:

注意: 如果subview的frame超过了superview的bounds, 默认超出的部分是不会被裁剪掉的,就是会被显示出来,如果需要被裁剪,设置superview中的clipsToBounds属性为YES即可.

A subview whose frame lies outside of its superview’s visible bounds is not clipped by default. If you want your subview to be clipped to the superview’s bounds, you must explicitly set the clipsToBounds property of the superview to YES.

Locating Views in a View Hierarchy

在view hirearchy中获取一个view, 通过如下方法:

  • 存一个指向这个view的指针

  • 给view标识一个tag, 通过viewWithTag:方法获取它

There are two ways to locate views in a view hierarchy:

  • Store pointers to any relevant views in an appropriate location, such as in the view controller that owns the views.

  • Assign a unique integer to each view’s tag property and use the viewWithTag: method to locate it.

Translating, Scaling, and Rotating Views

通过view中的transform属性来旋转


// M_PI/4.0 is one quarter of a half circle, or 45 degrees. CGAffineTransform xform = CGAffineTransformMakeRotation(M_PI/4.0); self.view.transform = xform;

Converting Coordinates in the View Hierarchy

坐标转换

convertPoint:fromView:

convertRect:fromView:

convertPoint:toView:

convertRect:toView:

convertPoint:fromWindow:

convertRect:fromWindow:

convertPoint:toWindow:

convertRect:toWindow:

UIKit View PG的更多相关文章

  1. 【IOS笔记】View Controller Basics

    View Controller Basics   视图控制器基础 Apps running on iOS–based devices have a limited amount of screen s ...

  2. Postgresql - MATERIALIZED VIEW

    MATERIALIZED VIEWPG 9.3 版本之后开始支持物化视图.View 视图:虚拟,不存在实际的数据,在查询视图的时候其实是对视图内的表进行查询操作. 物化视图:实际存在,将数据存成一张表 ...

  3. 关于UIView需要看的一些官方文档

    View Controller PG(Programming Guide)  看过一遍 View PG 正在看 Drawing and Printing PG Quartz 2D PG 更高级的cus ...

  4. Android 屏蔽ScrollView滑动操作

    屏蔽ScrollView滑动操作,如下,会用到ViewConfiguration这个类,这个类可以获取到用户是否为滑动操作的临界值. 代码如下: package com.xx.uikit.view; ...

  5. UIWindow及程序启动的过程

    1.   UIWindow才有自发显示的功能, 一个程序之所以能显示东西,是因为有window !//  [self.window makeKeyAndVisible]; 2.   任何view的显示 ...

  6. SwiftUI学习(二)

    教程 2 - Building Lists and Navigation Section 4 - Step 2: 静态 List var body: some View { List { Landma ...

  7. mvp设计模式

    一.设计模式的简单介绍 MVP的 V 层是由UIViewController 和UIView 共同组成view 将委托presenter 对它自己的操作,(简单来说就是presenter发命令来控制v ...

  8. 解析SwiftUI布局细节(三)地图的基本操作

    前言 前面的几篇文章总结了怎样用 SwiftUI 搭建基本框架时候的一些注意点(和这篇文章在相同的分类里面,有需要了可以点进去看看),这篇文章要总结的东西是用地图数据处理结合来说的,通过这篇文章我们能 ...

  9. iOS UIKit:view

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css); @import url(/ ...

随机推荐

  1. Spring源码:IOC原理解析(一)

    版权声明:本文为博主原创文章,转载请注明出处,欢迎交流学习! IOC(Inversion of Control),即控制反转,意思是将对象的创建和依赖关系交给第三方容器处理,我们要用的时候告诉容器我们 ...

  2. mybatis学习笔记(四)-- 为实体类定义别名两种方法(基于xml映射)

    下面示例在mybatis学习笔记(二)-- 使用mybatisUtil工具类体验基于xml和注解实现 Demo的基础上进行优化 以新增一个用户为例子,原UserMapper.xml配置如下: < ...

  3. (转载)WebSphere MQ安装过程

    参考文档: http://www.ibm.com/developerworks/cn/linux/linux-speed-start/l-ss-mq/

  4. Apache+Tomcat实现动静分离

    完成Tomcat集群搭建后,我们只需修改两.三处即可实现动静分离. 1.将原来httpd.conf中JkMount的路由规则都放入conf/extra/httpd-urimap.conf中: /*=l ...

  5. server

  6. Delphi临界区的使用

    在开发一个平板点餐软件后台订单打印程序时,使用线程订单打印,为防打印阻塞使用临界区. 类: type MYPARA=record title:pchar; //标题 str:pchar; flag:i ...

  7. 【JS小技巧】JavaScript 函数用作对象的隐藏问题

    用户反馈 @消失的键盘 在论坛反馈了一个问题,在 AppBoxMvc 中的 Title 模型中,如果将 Name 属性改名为小写的 name 属性,就会报错: 因为这是一个 ASP.NET MVC 的 ...

  8. Linux 下搭建www服务器

    偶然的机会接触了前端开发,尽管最初的意愿是后台. 不过现在看来,前端后台数据库密不可分! 回想起来感觉自己学习的层次也还很好,因为之前有学习c语言.c++的基础,所以在学习html,js的过程中感觉还 ...

  9. POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat 二分)

    POJ 2296 Map Labeler / ZOJ 2493 Map Labeler / HIT 2369 Map Labeler / UVAlive 2973 Map Labeler(2-sat ...

  10. 微信小程序怎么用?线下商家最适合玩小程序

    随着微信小程序不断地释放新功能,许多行业越来越关注小程序,目前已经有不少餐饮和线下传统零售企业开始谋划利用好小程序.但是,线下商业有着复杂的场景,如何针对自己行业的特点和需求开发出属于自己的小程序,是 ...