UIKit (1) ---- UIView

[TOC]

一、官方文檔簡介

The UIView class defines a rectangular area on the screen and the interfaces for managing the content in that area. At runtime, a view object handles the rendering of any content in its area and also handles any interactions with that content. The UIView class itself provides basic behavior for filling its rectangular area with a background color. More sophisticated content can be presented by subclassing UIView and implementing the necessary drawing and event-handling code yourself.

二、Creating a View

CGRect  viewRect = CGRectMake(10, 10, 100, 100);
UIView* myView = [[UIView alloc] initWithFrame:viewRect];
  • 這個origin坐標是相對上一級視圖(super view)坐標系的
  • 插入試圖方法
    • addSubview: Add a subview to another view, places the specified view on top of other siblings.
    • insertSubview:aboveSubview:Inserts a view above another view in the view hierarchy.
    • insertSubview:belowSubview:Inserts a view below another view in the view hierarchy.
    • exchangeSubviewAtIndex:withSubviewAtIndex:Exchanges the subviews at the specified indices.

注意:UIView默認是透明的,如果不加背景色的話,即使設(shè)置alpha值也體現(xiàn)不出View的層級關(guān)系。

三、常用的屬性和方法

  1. 屬性和說明

  • CGRect frame; 控件的位置和大小,所有的控件必須指定這個屬性,否則即使有控件也無法顯示
  • CGRect bounds; 當前控件位置和大小,但是和frame不同的是它的位置是確定的(0,0)
  • CGPoint center; 控件的中心位置,一般用戶進行控件定位
  • CGAffineTransform transform; 控件矩陣變化,包括平移、縮放、旋轉(zhuǎn),默認CGAffineTransformIdentity
  • UIViewAutoresizing autoresizingMask; 控件旋轉(zhuǎn)時大小自動伸縮,默認為UIViewAutoresizingNone
  • UIView *superview; 當前控件的父控件
  • NSArray *subviews; 當前控件的所有一級子控件,注意其子控件的子控件并不包括在內(nèi)
  • BOOL hidden; 是否隱藏,默認為NO
  • UIViewContentMode contentMode; 內(nèi)容模式,主要用于指定控件內(nèi)容(注意不是子控件)如何填充,一般UIImageView經(jīng)常使用,默認為UIViewContentModeScaleToFill
  • NSInteger tag; 控件的標示,可以存儲一些和當前控件有關(guān)的信息(但是注意只能是整形),默認為0

注意:contentMode屬性是一個枚舉類型

typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill, //內(nèi)容按Super比例填充
UIViewContentModeScaleAspectFit, //內(nèi)容按保持比例不完全填充 contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill, //內(nèi)容保持比例完全填充,填充內(nèi)容有溢出 contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter, // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};

2. ##方法和說明
  * `-(void)addSubview:(UIView *)view;` 添加子控件
  * `-(void)removeFromSuperview;`   從父控件中移除當前控件
  * `-(void)insertSubview:(UIView *)view atIndex:(NSInteger)index;` 在指定位置插入子控件
  * `+(void)beginAnimations:(NSString *)animationID context:(void *)context;`   開始一段動畫
  * `+(void)commitAnimations; `  結(jié)束一段動畫,注意在開始和結(jié)束之間如果控件的某些屬性發(fā)生變化iOS將以動畫方式進行改變
  * `+(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0);`  以block的形式執(zhí)行一段動畫,注意這個方法有幾種相關(guān)的重載
  * `-(void)addGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer NS_AVAILABLE_IOS(3_2);`    添加手勢操作
  * `-(void)removeGestureRecognizer:(UIGestureRecognizer*)gestureRecognizer NS_AVAILABLE_IOS(3_2);` 移除手勢操作

3. ##官方文檔屬性摘錄

  **Configuring a View’s Visual Appearance**  視圖的外表樣式

  * `UIColor backgroundColor`   The view’s background color.  背景色
  * `BOOL hidden`  A Boolean value that determines whether the view is hidden.  是否隱藏
  * `CGFloat alpha`  The view’s alpha value.浮點數(shù)0.0~1.0,0代表透明,1代表不透明
  * `BOOL opaque`  A Boolean value that determines whether the view is opaque.  是否不透明
  * `UIColor tintColor`  The first nondefault tint color value in the view’s hierarchy, ascending from and starting with the view itself.  
  * `tintAdjustmentMode`  The first non-default tint adjustment mode value in the view’s hierarchy, ascending from and starting with the view itself.
  * `BOOL clipsToBounds`  A Boolean value that determines whether subviews are confined to the bounds of the view.
  * `BOOL clearsContextBeforeDrawing`  A Boolean value that determines whether the view’s bounds should be automatically cleared before drawing.
  * `UIView maskView`An optional view whose alpha channel is used to mask a view’s content.通道蒙版
  * `(class) layerClass`  The class used to create the layer for instances of this class.
  * `CALayer layer`  The view’s Core Animation layer used for rendering.  渲染的核心視圖核心動畫層,view是layer的代理

  **Configuring the Bounds and Frame Rectangles**  設(shè)置

  * `frame`  
The frame rectangle, which describes the view’s location and size in its superview’s coordinate system.
  * `bounds`
The bounds rectangle, which describes the view’s location and size in its own coordinate system.
  * `center`
The center of the frame.
  * `transform`
Specifies the transform applied to the receiver, relative to the center of its bounds.

  **Configuring the Event-Related Behavior**  配置事件關(guān)聯(lián)行為

  *  `BOOL userInteractionEnabled`
A Boolean value that determines whether user events are ignored and removed from the event queue.
  * `multipleTouchEnabled`
A Boolean value that indicates whether the receiver handles multi-touch events.
  * `exclusiveTouch`
A Boolean value that indicates whether the receiver handles touch events exclusively.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,940評論 0 23
  • PLEASE READ THE FOLLOWING APPLE DEVELOPER PROGRAM LICENSE...
    念念不忘的閱讀 13,552評論 5 6
  • #孤單物品離別站# 每次來到正佳廣場吃午餐晚餐,飯后的甜點一定是Hi百貨,也就是在昨天晚上,依舊跟著這個程序走,卻...
    wuli夾心閱讀 354評論 0 0
  • 你 把四季的風景 幻成溫柔的眸 深情凝視每一個愿望 她 卻總是這樣 高凌凌 冷冷地蔑笑 不如 把所有輕賤的柔情全丟...
    坤哥_b463閱讀 216評論 0 0
  • 《康熙王朝》是由二月河的歷史小說《康熙大帝》改編而成,所以劇情有多處與歷史不符,但這也不能阻擋它成為一部優(yōu)秀的歷史...
    愛讀書的仙女a(chǎn)sd閱讀 1,703評論 0 1