UIView是屏幕上矩形面積的界面,用來管理界面上的內容
有以下幾點:
繪圖與動畫
佈局和子視圖管理
事件處理
視圖可以嵌套 形成父-子視圖
通常情況下 子視圖不是父視圖的一部分,也就是說,子視圖會覆蓋父視圖相關區域,超出父視圖面積的部分也將顯示.
可以通過改變屬性clipsToBounds
來取出超出部分的顯示,如下:
view添加view,並剪邊(UIView屬性clipsTobounds的應用)
view1添加view2到其中,如果view2大於view1,或者view2的座標不在view1的範圍內,view2是蓋着view1的,
UIView有一個屬性,clipsTobounds 默認情況下是NO,
如果,我們想要view2把超出的那部份隱藏起來的話,就得改變它的父視圖也就view1的clipsTobounds屬性值。
view1.clipsTobounds = YES;
很簡單吧,困擾了我好長時間,感謝小三子提供的代碼,一不小心發現了。
frame:定義view的在父view系統座標系中的原始尺寸,通常用來佈局view的大小
bounds:在不改變大小的情況下,用來調整view的位置
center:定了view的內部尺寸,專門應用與自定義區域的繪製代碼
The
size portion of the frame and bounds rectangles are coupled together so that changing the size of either rectangle updatesthe size of both.
一個矩形區域的大小和邊界通常結合起來使用,以改變和更新矩形的大小和位置
創建View
CGRect viewRect = CGRectMake(10, 10, 100, 100); |
UIView* myView = [[UIView alloc] initWithFrame:viewRect]; |
增加成爲父view的子view
exchangeSubviewAtIndex:withSubviewAtIndex:
創建一個view時,重要的一點是,要給autoresizingMask這個屬性制定一個合適的值,用於view自動調整尺寸.舉個例子,就是你如果有很多view疊在一起,如果想同時放大縮小它們,可以用這個方法.
For example, calling the setNeedsLayout
method
forces your view to update its layout.
當調用方法setNeedsLayout時,視圖會自動重繪.
視圖的重繪週期
在以下情況下會重繪:
view第一次顯示時
view的部分或所有可見性改變導致佈局發生變化時
系統要求view重新繪製內容時
For views that contain custom content using UIKit or Core Graphics, the system calls the view’s drawRect:
method.
當view包含自定義內容時,系統調用view的函數drawRect:進行繪製.
需要自定義繪圖時,子類需要重寫drawRect:方法
當view的真實內容發生改變時,你需要通知系統去重繪.you
do this by calling your v iew’ssetNeedsDisplay
or setNeedsDisplayInRect:
method
of the view.
這兩個方法讓系統知道,視圖需要立即更新(在下一次更新週期前,正常情況下,view是等待下一次更新週期進行更新的).
Note: If you are using OpenGL ES to do your drawing, your view’s drawRect:
method
is not called. Instead, it is up to you to determine when your view needs to be redrawn and initiate the appropriate drawing updates. For more information about how to draw using OpenGL ES, see OpenGL
ES Programming Guide for iOS.
改變幾個屬性,就可以讓view具有動畫效果.改變一些屬性創建動畫,並將這些改變在短時間內傳達給用戶.
UIView自身對動畫做了很多工作,但是你仍然需要決定決定改變哪個屬性,需要哪種動畫.
There are two different ways to initiate animations:
-
In iOS 4 and later, use the Block-based animation methods. (Recommended)(such as
animateWithDuration:animations:
) -
Use the begin/commit animation methods. (you must use the
beginAnimations:context:
andcommitAnimations
class methods to mark the beginning and ending of your animations.)
以下這些屬性是可動畫的:
The following properties of the UIView
class
are animatable:
線程的考慮
您的應用程序的用戶界面的操作必須出現在主線程。因此,你應該始終在應用程序的主線程中運行的代碼中調用UIView的方法。唯一的例外是在創建視圖對象本身時,但所有其他的操作應該發生在主線程上。
子類化的注意事項
UIView是個高度化可配置的類
子類化的時候 需要根據實際需求覆蓋一些方法.
可以覆蓋的方法有以下幾方面:
-
Initialization:
-
initWithFrame:
—It is recommended that you implement this method. You can also implement custom initialization methods in addition to, or instead of, this method. -
initWithCoder:
—Implement this method if you load your view from an Interface Builder nib file and your view requires custom initialization. -
layerClass
—Implement this method only if you want your view to use a different Core Animation layer for its backing store. For example, if you are using OpenGL ES to do your drawing, you would want to override this method and return theCAEAGLLayer
class.
-
-
Drawing and printing:
-
drawRect:
—Implement this method if your view draws custom content. If your view does not do any custom drawing, avoid overriding this method. -
drawRect:forViewPrintFormatter:
—Implement this method only if you want to draw your view’s content differently during printing.
-
-
Layout:
-
sizeThatFits:
—Implement this method if you want your view to have a different default size than it normally would during resizing operations. For example, you might use this method to prevent your view from shrinking to the point where subviews cannot be displayed correctly. -
layoutSubviews
—Implement this method if you need more precise control over the layout of your subviews than the autoresizing behaviors provide. -
didAddSubview:
,willRemoveSubview:
—Implement these methods as needed to track the additions and removals of subviews. -
willMoveToSuperview:
,didMoveToSuperview
—Implement these methods as needed to track the movement of the current view in your view hierarchy. -
willMoveToWindow:
,didMoveToWindow
—Implement these methods as needed to track the movement of your view to a different window.
-
-
Event Handling:
-
touchesBegan:withEvent:
,touchesMoved:withEvent:
,touchesEnded:withEvent:
,touchesCancelled:withEvent:
—Implement these methods if you need to handle touch events directly. (For gesture-based input, use gesture recognizers.)
-
子類化的候選方案
大部分類的行爲是可配置的,不需要子類化,在子類化之前,考慮以下屬性和行爲是否能提供滿足你的需要:
-
Set a value for the
autoresizingMask
property. This property provides automatic layout behavior when the superview’s frame changes. -
Set a value for the
contentMode
property. This property determines the layout behavior of the view’s content. This property also affects how the content is scaled to fit the view and whether it is cached or redrawn. -
Set a value for the
contentStretch
property. This property is typically used to implement buttons and other resizable views. Defining a stretchable area lets the system handle any drawing associated with size changes to the view. -
Set a value for the
hidden
oralpha
property. These properties change the transparency of the view as a whole and avoid the need for drawing your content with transparency. -
Set a value for the
backgroundColor
property. This property sets the view’s overall color and avoids the need to draw that color yourself in thedrawRect:
method. -
Add subviews. Rather than draw your content using a
drawRect:
method, embed image and label subviews with the content you want to present. -
Add one or more gesture recognizers. Rather than subclass to intercept and handle touch events yourself, you can use gesture recognizers to send anaction message to a target object.
-
Use the built-in animation support rather than trying to animate changes yourself. The animation support provided by Core Animation is fast and easy to use.
-
Use an image for the view’s background. For views that display relatively static content, consider using a
UIImageView
object with gesture recognizers instead of subclassing and drawing the image yourself. Alternatively, you can also use a genericUIView
object and assign your image as the content of the view’sCALayer
object.
Tasks
Initializing a View Object
Configuring a View’s Visual Appearance
-
backgroundColor
property -
hidden
property -
alpha
property -
opaque
property -
clipsToBounds
property -
clearsContextBeforeDrawing
property -
+ layerClass
-
layer
property
Configuring Event-Related Behavior
-
userInteractionEnabled
property -
multipleTouchEnabled
property -
exclusiveTouch
property
Configuring the Bounds and Frame Rectangles
Configuring the Resizing Behavior
-
autoresizingMask
property -
autoresizesSubviews
property -
contentMode
property -
contentStretch
property -
– sizeThatFits:
-
– sizeToFit
Managing the View Hierarchy
-
superview
property -
subviews
property -
window
property -
– addSubview:
-
– bringSubviewToFront:
-
– sendSubviewToBack:
-
– removeFromSuperview
-
– insertSubview:atIndex:
-
– insertSubview:aboveSubview:
-
– insertSubview:belowSubview:
-
– exchangeSubviewAtIndex:withSubviewAtIndex:
-
– isDescendantOfView:
Laying out Subviews
Drawing and Updating the View
Formatting Printed View Content
Managing Gesture Recognizers
Animating Views with Blocks
-
+ animateWithDuration:delay:options:animations:completion:
-
+ animateWithDuration:animations:completion:
-
+ animateWithDuration:animations:
-
+ transitionWithView:duration:options:animations:completion:
-
+ transitionFromView:toView:duration:options:completion:
Animating Views
Use of the methods in this section is discouraged in iOS 4 and later. Use the block-based animation methods instead.
-
+ beginAnimations:context:
-
+ commitAnimations
-
+ setAnimationStartDate:
-
+ setAnimationsEnabled:
-
+ setAnimationDelegate:
-
+ setAnimationWillStartSelector:
-
+ setAnimationDidStopSelector:
-
+ setAnimationDuration:
-
+ setAnimationDelay:
-
+ setAnimationCurve:
-
+ setAnimationRepeatCount:
-
+ setAnimationRepeatAutoreverses:
-
+ setAnimationBeginsFromCurrentState:
-
+ setAnimationTransition:forView:cache:
-
+ areAnimationsEnabled
Identifying the View at Runtime
-
tag
property -
– viewWithTag:
Converting Between View Coordinate Systems
Hit Testing in a View
Ending a View Editing Session
Observing View-Related Changes
-
– didAddSubview:
-
– willRemoveSubview:
-
– willMoveToSuperview:
-
– didMoveToSuperview
-
– willMoveToWindow:
-
– didMoveToWindow