UIKit View PG

View Program iOS

View and Window Architecture

The view draw cycle

When a view first appears onexpress

the screen, the system asks it to draw its content. The system captures a snapshot of this content and useswindows

that snapshot as the view’s visual representation. If you never change the view’s content, the view’s drawingapp

code may never be called again. The snapshot image is reused for most operations involving the view. If youide

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.ui

當你的view內容變化的時候,咱們不直接重繪它,而是經過setNeedsDisplaysetNeedsDisplayInRect:方法通知系統去幫咱們完成重繪。this

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.code

Content Mode

設置當view的形狀發生變化時,它的content如何重繪,例如圖片是拉伸,平鋪等。orm

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:

相關文章
相關標籤/搜索