iPhone 的 UIView的學習

fmms 12年前發布 | 24K 次閱讀 iPhone iOS開發 移動開發

概述

UIView對象在屏幕中定義了一個復雜區域和界面來管理這個區域的內容

視圖的職責:
畫圖和動畫。
布局和子視圖管理。

事件處理。

 

1、創建一個視圖對象

CGRect viewRect = CGRectMake(10,10,100,100);
UIView* myView = [[UIView alloc] initWithFrame:viewRect];
[self.window addSubview :myView];//將視圖作為子視圖添加到window中

2、動畫

改變一些視圖屬性將會使用到動畫,改變屬性時創建一個動畫,用于給用戶傳遞在較短時間內的變化。UIView類做了動畫展現的大部分工作,但是你仍然需要聲明哪種屬性改變的時候,你需要動畫效果。有兩種不同的類型來初始化動畫
下面的UIView屬性支持動畫:
frame,bounds,center,transform,alpha,backgroundColor,contentStretch
在iOS 4之后,使用block-based動畫方法(推薦使用)
使用 開始/提交方式(begin/commit)

3、管理視圖的層次結構

superview屬性:
subviews屬性:
window屬性:
-addSubview方法
-bringSubviewToFront:(UIView )veiw方法,將view視圖移到層次結構的最頂端,使得其得以展示
-sendSubviewToBack:(UIView
)veiw方法,和上面方法正好相反
-removeFromSupview方法,
-insertSubview:(UIView )view atIndex:(Interger)index方法
-insertSubview:(UIView
)view aboveSubview(UIView )siblingView 方法
-insertSubview:(UIView
)view belowSubview(UIView )siblingView 方法
-exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2方法
-isDescendantOfView:(UIView
)view方法,判斷view是不是指定視圖的子視圖

4、子視圖的布局(layout)

-layoutSubviews方法,這個方法,默認沒有做任何事情,需要子類進行重寫
-setNeedsLayout方法
-layoutIfNeeded方法,立即對子視圖進行布局

5、畫/更新視圖

-drawRect:(CGRect)rect方法
-setNeedsDisplay
-setNeedsDisplayInRect:(CGRect)invalidRect方法

6、以塊展現動畫的方式(animating views with block)

  • animateWithDuration:delay:options:animations:completion:
  • animateWithDuration:animations:completion:
  • animateWithDuration:animations:
  • transitionWithView:duration:options:animations:completion:

    + transitionFromView:toView:duration:options:completion:

    7、在視圖和坐標系統之間轉換

    -convertPoint:toView
    -convetPoint:fromView
    -convertRect:toView
    -convertRect:fromView

    8、跟蹤視圖相關的改變

    -didAddSubview:
    -willRemoveSubview:
    -willMoveToSuperview
    -didMoveToSuperview
    -willMoveToWindow:
    -didMoveToWindow
 本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!