iOS UIView初识

来源:互联网 发布:oracle的sql developer 编辑:程序博客网 时间:2024/05/16 09:15

  frame的概念

    // frame 代表了一个view在父坐标系中的位置 大小

    // frame 是一个结构体类型 CGRect类型,并且里面结构体成员也是结构体 origin size

    // origin CGPoint   x       y

    // size   CGSize   width  height

    注意: 如何 viewframe

    // 如果我们直接要该viewframe里面的x y 或者 width或者 height没有办法通过 view.frame.origin.x = xxx或者 view.frame.size.width = xx更改。

    // 因为 .语法出现了二义性两种含义

    // view.frame  此时点语法是 getter操作

    // origin.x  或者 size.width为结构体访问成员变量

    

    // 1. 我们可以先获取 viewframe

    // 2. 更改frame.origin.x

    // 3. 把更改后的 frame再赋值给 view.frame


    // 创建一个cgrect结构体使用 CGRectMake()函数创建

    CGRect rect1 = CGRectMake(200, 200, 50, 50);

    

    rect1.origin.x = 0;

    

    

    

    UIView *viewRect = [[UIView alloc] initWithFrame:rect1];

    

    

    viewRect.backgroundColor = [UIColor purpleColor];

    

    [self.window addSubview:viewRect];

    

    

    

  

    

    

    

    // 1. 获取viewRectframe

    CGRect rect2 = viewRect.frame;

    

    // 2. 更改 rect2

    rect2.origin.x = 300;

    

    // 3. 赋值给 viewRect

    viewRect.frame = rect2;

    

    

    

    // center 属性

    // center 中心点也是view的一个重要属性,表示view的中心。通常用来移动或者改变位置的时候使用

    

    

    CGPoint center = viewRect.center; // 获取

    

    center.x = viewRect.frame.size.width / 2;  // 更改

    

    center.y = viewRect.frame.size.height / 2;

    

    viewRect.center = center; // 重新赋值

    

     

    */

    

    

    /*

    // bounds 边界

    // bounds  也是view的一个属性,表示view能够显示出的范围

    // bounds CGRect类型(x,y ,width, height)

    // frame不同的是 bounds参考点为自身坐标系,表示view的左上角距离自身坐标系的距离。

    

    // bounds 默认值为00widthheight

    

    // view 自身的坐标系,如果没有改变则都为左上角为原点,坐标值为(00

    

    // bounds 如果改变,意味着,view视图自身的坐标系发生了变化,坐标向左移动,则 x值增大,反之减少。

    

    UIView *boundsView = [[UIView alloc] initWithFrame:(CGRectMake(50, 50, 200, 200))];

    

    boundsView.backgroundColor = [UIColor blueColor];

    

    [self.window addSubview:boundsView];

    

    

    NSLog(@"%@", NSStringFromCGRect(boundsView.bounds));

    

    

    // 获取bounds

    CGRect bounds =  boundsView.bounds;

    

    // 更改 坐标系原点

    bounds = CGRectMake(50, 50, 200, 200);

    

    // bounds 赋值

    boundsView.bounds = bounds;

    

    

    

    // boundsView添加一个红色的子视图

    UIView *redView = [[UIView alloc] initWithFrame:(CGRectMake(50, 50, 100, 100))];

    

    redView.backgroundColor = [UIColor redColor];

    

    // 添加,添加给 boundsView,作为boudsView的子视图

//    [boundsView addSubview:redView];

    

    

    // bounds  frame区别

    // bounds view视图左上角,距离自身坐标系的距离。

    // bounds 默认为(00,此时原点和视图左上角重合。

    // bounds 如果发生变化,不会影响view的位置,影响的是 view 子视图的 位置。

    

    // frame  view视图左上角,距离 父视图坐标系的距离。

    // frame 需要我们在给父视图添加子视图的时候指定。

    // frame  子视图在父坐标系中的位置

    // frame 发生变化,视图位置发生变化

    

    // 注意:如果bounds宽高发生变化,是基于视图的中心点发生变化的,如果宽度-100,结果是左边-50,右边-50,中心位置没有发生变化。

     */

    

    

    /*

    // UIView 的相关方法

    // (1) 添加视图方法

    

    // addSubView方法

    // addSubView 每次添加都是添加到了 subViews数组的最后面

    

    // 1. 创建view对象

    UIView *redView = [[UIView alloc] initWithFrame:(CGRectMake(100, 100, 100, 100))];

    

    // 2. 设置属性

    redView.backgroundColor = [UIColor redColor];

    

    // 3. 添加到window上面

    [self.window addSubview:redView];

    

    // 4. 释放

    [redView release];

    

    // self.window  addSubview 是把view 添加到了 window.subViews数组里面,数组容器有一个特点,自动管理内部元素引用计数 add增加引用计数 remove 减少引用计数

    

    // 打印 windowsubViews属性

//    NSLog(@"%ld", self.window.subviews.count);

    

    

    // insertSubView: atIndex:在制定下标处添加子视图,原位置视图后移,不会出现数组越界,如果超出下标则添加到最后。

    

    UIView *orangeView = [[UIView alloc] initWithFrame:(CGRectMake(150, 150, 100, 100))];

    

    orangeView.backgroundColor = [UIColor orangeColor];

    

    [self.window insertSubview:orangeView atIndex:1];

    

    // 释放

    [orangeView release];

    

    

    // insertSubView: aboveSubview:

    // 把一视图添加到某视图之上

    UIView *yellowView = [[UIView alloc] initWithFrame:(CGRectMake(200, 200, 100, 100))];

    

    yellowView.backgroundColor = [UIColor yellowColor];

    

    [self.window insertSubview:yellowView aboveSubview:orangeView];

    

    // 释放

    [yellowView release];

    

    

    

    // insertSubview: belowSubView:

    UIView *greenView = [[UIView alloc] initWithFrame:(CGRectMake(150, 250, 100, 100))];

    

    greenView.backgroundColor = [UIColor greenColor];

    

    [self.window insertSubview:greenView belowSubview:yellowView];

    

    [greenView release];

    

    

    

    // (2) UIView 管理视图层次方法

    

    // removeFromSuperview 从父视图上面 移除子视图

    

//    [greenView removeFromSuperview];

//    [yellowView removeFromSuperview];

//    [orangeView removeFromSuperview];

//    [redView removeFromSuperview];

    

    NSArray *viewArrays = self.window.subviews;

    for (UIView *view in viewArrays) {

        

        // 只移除黄色视图

        if (view.backgroundColor == [UIColor yellowColor]) {

            

            [view removeFromSuperview];

            

        }

        

    }

    

    

    // UIView 的重要属性

    // 常用的一些属性

    // hidden 显隐性设置  alpha透明度 superview父视图

    // subViews 子视图们  tag标记值

    

    // 1. 创建视图

    UIView *view = [[UIView alloc] initWithFrame:(CGRectMake(0, 300, 200, 200))];

    

    // 2. 设置属性

    view.backgroundColor = [UIColor redColor];

    

    view.hidden = NO; // no不隐藏,yes隐藏视图,默认为 NO

    

    view.alpha = 0.5; // 透明度, 0.0~1.0 数值,默认为1.0

    

    view.tag = 100; // 设置标记值,方便 父视图通过tag取该视图

    

    // view tag值不能为0因为 window tag 默认为0.

    // 通过 0 tag值,取出来的始终是 window

    

    // 添加

    [self.window addSubview:view];

    

    UIWindow *window = (UIWindow *)view.superview; // 父视图

    NSLog(@"%@", window);

    

    NSArray *array = view.subviews; // 子视图数组

    NSLog(@"%@", array);

    

    // 通过tag值获取子视图

    UIView *view2 = [self.window viewWithTag:100];

    

    // 改变view2的背景颜色

    view2.backgroundColor = [UIColor yellowColor];

    

    */

    

    

    // UILabel  标签视图

    

    // 作用:负责文字的展现(小段文字展现)

    

    // 使用步骤:

    // 1. 创建

    UILabel *label = [[UILabelalloc]initWithFrame:(CGRectMake(100,100,120, 100))];

    

    // 2. 设置属性

    

    label.text@"字符串文本内容";//文本内容

    

    label.textColor = [UIColorblueColor];// 文本颜色,默认为黑色

    

    label.font = [UIFontsystemFontOfSize:16];//文本字体

    

    label.lineBreakMode =NSLineBreakByCharWrapping;//换行模式

    

    label.numberOfLines =2;//行数,设置为0默认换行,一行不足则默认换行显示

    

    label.shadowColor = [UIColorredColor];

    

    label.shadowOffset = CGSizeMake(4, 4);

    

    

    // 3. 添加到父视图上

    [self.windowaddSubview:label];



0 0
原创粉丝点击