UI基础之UIView

 MVC:是一种设计模式

    M(model):存储数据

    V(view) :展示数据 (UIView)

    C(controler):用来衔接MVC,作为MV之间的桥梁,负责数据的处理和传递等等 (UIViewController)

    CGPoint:用来设置坐标;

    CGSize: 用来设置尺寸;

    CGRect: 用来设置坐标和尺寸;

    

    frame:以父视图作为参考系,

   bounds:以自身作为参考系,坐标始终是(00

 

@property (strong, nonatomic) UIWindow *window;

//在应用程序代理中实例化,一个窗口,多个视图,app只有一个UIWindowUIWindow是根基

 

//创建一个自己的view

    CGRect frame2=CGRectMake((375-150)/2.0,(667-150)/2.0 , 150, 150);

    UIView *view=[[UIView alloc] initWithFrame:frame2];

    

    //将当前视图添加到window上面才能显示

    [self.window addSubview:view];

    

    //为当前视图设置背景色

    view.backgroundColor=[UIColor grayColor];

 

//定义一个人根视图

    _rootView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kheight)];

    _rootView.backgroundColor=[UIColor grayColor];

    [self.window addSubview:_rootView];

    

    //

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

    //可用tag值来找到相应视图

    view1.tag=1001;

    view1.backgroundColor=[UIColor redColor];

    [_rootView addSubview:view1];

    

    UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 150, 150)];

    view2.tag=1002;

    view2.backgroundColor=[UIColor blackColor];

    [_rootView addSubview:view2];

    

    //获取所有子视图

    NSArray *subviews=[_rootView subviews];

    NSLog(@"%@",subviews);

    

    //remove移除子视图

//    [view2 removeFromSuperview];

    

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

    view3.tag=1003;

    view3.backgroundColor=[UIColor greenColor];

    [_rootView addSubview:view3];

    

    //

    UIView *view4=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 160)];

    view4.backgroundColor=[UIColor yellowColor];

    

//    [_rootView insertSubview:view4 atIndex:1];

//    [rootView insertSubview:view4 aboveSubview:view1];

//    [rootView insertSubview:view4 belowSubview:view2];

//    [rootView bringSubviewToFront:view1]; 图层移到最顶层

//    [rootView sendSubviewToBack:view4];   图层移到最底层

    

    //0是子视图第一层

//    [rootView exchangeSubviewAtIndex:2 withSubviewAtIndex:0];

//    把两个索引对应的视图调换位置

    

    

    UIViewController *vc=[[UIViewController alloc] init];

    self.window.rootViewController=vc;

    

 

    

    return YES;

}

//点击空白处触发

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    //获取所有子视图

    NSArray *subView= _rootView.subviews;

    //取出所有子视图的第一个

//    UIView *lastView=[subView lastObject];

//    UIView *firstView=[subView firstObject];

    

    //每次将第一移动到最后

//    [_rootView sendSubviewToBack:lastView];

    

    //每次点击第一个和最后一个互换

//    [_rootView exchangeSubviewAtIndex:0 withSubviewAtIndex:subView.count-1];

    

    

    //通过tag值来控制颜色变化

    UIView *view=[_rootView viewWithTag:1003];

//    view.backgroundColor=[UIColor blackColor];

    //改变透明度

//    view.alpha-=0.1;

    //方法一隐藏掉,而不是remove掉,还存在

//    view.alpha=0;

    //方法二

    //[view removeFromSuperview];

    //方法三

    //view.alpha=1-view.alpha;

    //方法四

//    if (view.hidden==YES) {

//        view.hidden=NO;

//    }else{

//        view.hidden=YES;

//    }

    //方法五

//    view.hidden=!view.hidden;

 

 

原文地址:https://www.cnblogs.com/gzoof/p/5483070.html