UIKit (1) ---- UIView

2016-10-10  本文已影响0人  quantiza

[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];

注意:UIView默认是透明的,如果不加背景色的话,即使设置alpha值也体现不出View的层级关系。

三、常用的属性和方法

  1. 属性和说明

注意:contentMode属性是一个枚举类型

typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill, //内容按Super比例填充
UIViewContentModeScaleAspectFit, //内容按保持比例不完全填充 contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill, //内容保持比例完全填充,填充内容有溢出 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; `  结束一段动画,注意在开始和结束之间如果控件的某些属性发生变化iOS将以动画方式进行改变
  * `+(void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0);`  以block的形式执行一段动画,注意这个方法有几种相关的重载
  * `-(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.浮点数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**  设置

  * `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**  配置事件关联行为

  *  `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.
上一篇 下一篇

猜你喜欢

热点阅读