UIView中文部分翻譯

UIView本來想翻譯完的,可是翻着翻着就懶了。有時間再翻翻。


UIView Class Reference

Inherits from    
UIResponder : NSObject
Conforms to    
NSCoding
NSObject (NSObject)
Framework    
/System/Library/Frameworks/UIKit.framework
Availability    
Available in iOS 2.0 and later.
Companion guide    
View Programming Guide for iOS
Declared in    
UIPrintFormatter.h
UITextField.h
UIView.h
Related sample code    
iPhoneCoreDataRecipes
iPhoneMixerEQGraphTest
MoveMe
Touches
UICatalog

OverView
UIView類定義了在一個屏幕上一個矩形的區域並且定義了一些管理在這個區域內內容的接口。在運行時,這個view對象處理在這個區域內的內容的渲染,也提供處理內容的接口。
UIView類本身爲帶背景顏色的填充區域提供基本的動作。
因爲view對象是你的程序和用於互動的主要途徑,這些view有一系列的責任。
繪畫和動畫
佈局和子view的管理
一個view可以包含0或者多個子view
每一個view都定義他自己相對於他的父view的默認resizing行爲
一個可以手動改變他的size和position。
事件處理:
一個view是一個響應器並且處理觸摸事件和別的在UIResponder類中定義的事件。
views能夠用 addGestureRecognizer: 方法去安裝手勢識別器去出去一般的手勢。

一個父view可能包含任意數目的子view,但是每一個子view只能有且只有一個superview(負責合適的放置它的子view的位置)。
View的幾何性質Frame,bounds,center。frame的size接口和bounds的矩形是綁定一起的,所以改變任何矩形的size都會更新他們兩個的size。
Creating a View
用代碼編程創建一個view,可以用下面類似的代碼:
CGRect  viewRect = CGRectMake(10, 10, 100, 100);
UIView* myView = [[UIView alloc] initWithFrame:viewRect];
爲了給你的view添加一個子view,你可以用adsSubView:方法。這個addSubView:方法放置特定的view到別的兄弟view的最高層。你可以制定一個子view
的Z-次序的相對管理,通過用 insertSubview:aboveSubview: and insertSubview:belowSubview: 去添加它的時候。你可以交換已經存在的子views的位置
通過用exchangeSubviewAtIndex:withSubviewAtIndex:方法。
當創建一個view的時候,去適當的賦一個合適的值到 autoresizingMask屬性以確保這個view可以重新正確的賦值size。view自動賦值size主要發生在當你程序的方向改變時。
但是它也經常發生在別的時候。比如調用setNeedsLayout 去強制更新它的佈局的時候。
The View Drawing Cycle
The UIView class uses an on-demand drawing model for presenting content. When a view first appears on the screen, the system asks it to draw its content. If you never change the view’s content, the view’s drawing code may never be called again.If you do change the view’s content, you do not redraw the view’s contents directly. Instead, you invalidate the view using either the setNeedsDisplay or setNeedsDisplayInRect: method and let the system redraw its contents later.
Note: Changing a view’s geometry does not automatically cause the system to redraw the view’s content.
For custom UIView subclasses, you typically override the drawRect: method of your view and use that method to draw your view’s content. There are also other ways to provide a view’s content, such as setting the contents of the underlying layer directly, but overriding drawRect: is the most common technique.

Content Modes
The content mode of a view is applied whenever you do the following:

Change the width or height of the view’s frame or bounds rectangles.
Assign a transform that includes a scaling factor to the view’s transform property.
By default, the contentMode property for most views is set to UIViewContentModeScaleToFill,which causes the view’s contents to be scaled to fit the new frame size。As you can see from the figure, not all content modes result in the view’s bounds being filled entirely, and those that do may distort that content.
其中使用UIViewContentModeScaleToFill會使圖片變形Distort

Stretchable Views
 The stretchable area you specify can allow for stretching along one or both axes of the view. Of course, when stretching a view along two axes, the edges of the view must also define a repeatable pattern to avoid any distortion.
You specify the stretchable portion of a view using the contentStretch property.
 Stretchable areas are only used when the content mode would cause the view’s content to be scaled. This means that you stretchable views are supported only with the UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, and UIViewContentModeScaleAspectFill content modes. If you specify a content mode that pins the content to an edge or corner (and thus does not actual scale the content), the view ignores the stretchable area.

Built-In Animation Support
To perform an animation for one of these animatable properties, all you have to do is:
Tell UIKit that you want to perform an animation.
Change the value of the property.
Among the properties you can animate on a UIView object are the following:

frame - You can use this to animate position and size changes for the view.
bounds - Use this to animate changes to the size of the view.
center - Use this to animate the position of the view.
transform - Use this to rotate or scale the view.
alpha - Use this to change the transparency of the view.
backgroundColor - Use this to change the background color of the view.
contentStretch - Use this to change how the view’s contents stretch.
In addition to the animations you create using UIKit classes, you can also create animations using Core Animation layers. Dropping down to the layer level gives you much more control over the timing and properties of your animations.
here are two different ways to initiate animations:

In iOS 4 and later, use the block-based animation methods. (Recommended)
Use the begin/commit animation methods.
The block-based animation methods (such as animateWithDuration:animations:) greatly simplify the creation of animations. With one method call, you specify the animations to be performed and the options for the animation. However, block-based animations are available only in iOS 4 and later. If your application runs on earlier versions of iOS, you must use the beginAnimations:context: and commitAnimations class methods to mark the beginning and ending of your animations.

Methods to Override

When subclassing UIView, there are only a handful of methods you should override and many methods that you might override depending on your needs


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章