美文网首页
UIKit (1) ---- UIView

UIKit (1) ---- UIView

作者: quantiza | 来源:发表于2016-10-10 19:15 被阅读0次

    [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默认是透明的,如果不加背景色的话,即使设置alpha值也体现不出View的层级关系。

    三、常用的属性和方法

    1. 属性和说明

    • CGRect frame; 控件的位置和大小,所有的控件必须指定这个属性,否则即使有控件也无法显示
    • CGRect bounds; 当前控件位置和大小,但是和frame不同的是它的位置是确定的(0,0)
    • CGPoint center; 控件的中心位置,一般用户进行控件定位
    • CGAffineTransform transform; 控件矩阵变化,包括平移、缩放、旋转,默认CGAffineTransformIdentity
    • UIViewAutoresizing autoresizingMask; 控件旋转时大小自动伸缩,默认为UIViewAutoresizingNone
    • UIView *superview; 当前控件的父控件
    • NSArray *subviews; 当前控件的所有一级子控件,注意其子控件的子控件并不包括在内
    • BOOL hidden; 是否隐藏,默认为NO
    • UIViewContentMode contentMode; 内容模式,主要用于指定控件内容(注意不是子控件)如何填充,一般UIImageView经常使用,默认为UIViewContentModeScaleToFill
    • NSInteger tag; 控件的标示,可以存储一些和当前控件有关的信息(但是注意只能是整形),默认为0

    注意: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.

    相关文章

      网友评论

          本文标题:UIKit (1) ---- UIView

          本文链接:https://www.haomeiwen.com/subject/dffvyttx.html