UIView

//command+R   运行

//command+.   停止

//command+B   预编译

 

//command+12模拟器大小

//command+shift+h  home

//command+shift+两个h   后台

 

    //所有控件的父类

    //实例化方式,属性,相关方法

    

    //实例化:alloc initWithFrame

    //CGRect类型:赋值方式CGRectMake

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

    view.frame = CGRectMake(100, 50, 200, 100);

    //位置及大小:frame

    //坐标系的原点、正轴方向

    //位置:前两个参数    原点:父视图最左上角的点   x轴正轴方向:向右   y轴正轴方向:向下

    //开发过程中的单位:像素

    //CGRectMake(CGFloat x (viewx轴的位置), CGFloat y (viewy轴的位置), CGFloat width (view本身自己的宽), CGFloat height (view本身自己的高))

    //CGRect本身是个结构体->CGPoint结构体、CGSize结构体

    //CGPoint->x位置的结构体

    //CGSize->widthheight  大小的结构体

 

    //设置背景颜色:backgroundColor

    //赋值方式一:[UIColor someColor]

    /*

     + (UIColor *)blackColor;      // 0.0 white

     + (UIColor *)darkGrayColor;   // 0.333 white

     + (UIColor *)lightGrayColor;  // 0.667 white

     + (UIColor *)whiteColor;      // 1.0 white

     + (UIColor *)grayColor;       // 0.5 white

     + (UIColor *)redColor;        // 1.0, 0.0, 0.0 RGB

     + (UIColor *)greenColor;      // 0.0, 1.0, 0.0 RGB

     + (UIColor *)blueColor;       // 0.0, 0.0, 1.0 RGB

     + (UIColor *)cyanColor;       // 0.0, 1.0, 1.0 RGB

     + (UIColor *)yellowColor;     // 1.0, 1.0, 0.0 RGB

     + (UIColor *)magentaColor;    // 1.0, 0.0, 1.0 RGB

     + (UIColor *)orangeColor;     // 1.0, 0.5, 0.0 RGB

     + (UIColor *)purpleColor;     // 0.5, 0.0, 0.5 RGB

     + (UIColor *)brownColor;      // 0.6, 0.4, 0.2 RGB

     + (UIColor *)clearColor;      // 0.0 white, 0.0 alpha

     */

    //赋值方式二:[UIColor colorWithRed:(CGFloat(0.0-1.0)) green:(CGFloat(0255的数)/255.0)) blue:(CGFloat) alpha:(CGFloat)]

//    view.backgroundColor = [UIColor redColor];

//    view.backgroundColor = [UIColor colorWithRed:0.45 green:0.23 blue:0.67 alpha:1.0];

    view.backgroundColor = [UIColor colorWithRed:123.0/255.0 green:213/255.0 blue:145/255.0 alpha:1.0];

    

    //透明度:alpha   0.0-1.0  0.0:完全透明   1.0:完全不透明,默认状态

    view.alpha = 0.6;

    

    //隐藏属性:hidden  BOOL  NO:不隐藏,默认状态   YES:隐藏

    view.hidden = NO;

    

    //view加载到window层上:addSubview

    //父视图、子视图:不是继承关系、不是绝对的概念

    //[父视图 addSubview:子视图]

    [self.window addSubview:view];

    

    //实例化

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

    view1.backgroundColor = [UIColor redColor];

    //添加到父视图上面

    [view addSubview:view1];

    UIView * view2 = [[UIView alloc]init];

    view2.bounds = CGRectMake(0, 0, 30, 30);

    view2.backgroundColor = [UIColor yellowColor];

    [view1 addSubview:view2];

// ************************************************

    //实例化

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

    

    //设置背景颜色

    view.backgroundColor = [UIColor redColor];

    

    //设置透明度

    //透明度:父视图有透明度,它上面的子视图就会有相应的透明度;子视图的透明度对父视图没有影响。

    view.alpha = 1.0;

    

    //设置隐藏属性

    //隐藏属性:父视图隐藏,子视图跟着隐藏;子视图的隐藏属性对父视图没有影响。

    view.hidden = NO;

    

    //tag

    view.tag = 1000;

    

    //添加到父视图上面

    [self.window addSubview:view];

    

    //创建一个view的子视图

    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];

    view2.backgroundColor = [UIColor blueColor];

    view2.alpha = 1.0;

    view2.hidden = YES;

    //添加到view上面

    [view addSubview:view2];

    

    //tag找到相应的view

    UIView *viewTag = (UIView *)[self.window viewWithTag:1000];

    NSLog(@"view=%@",view);

    NSLog(@"tag=%@",viewTag);

    /*

    view=<UIView: 0x78f544e0; frame = (50 100; 220 220); tag = 1000; layer = <CALayer: 0x78f53110>>

    tag =<UIView: 0x78f544e0; frame = (50 100; 220 220); tag = 1000; layer = <CALayer: 0x78f53110>>

     */

    /******UIView-中心点*****/
    UIView *view = [[UIView alloc] init];
    //位置及大小的设置方式一:frame
//    view.frame = CGRectMake(100, 100, 120, 120);
    //位置及大小的设置方式二:center(中心点,位置),bounds(大小)
    
    //center:view自己的中心点
    view.center = CGPointMake(160, 70);
    //bounds:后两个参数是view自己本身的大小
    //bounds:前两个参数影响的是view自己本身的坐标系:正数坐标系原点向左和向上移动;父数坐标系原点向右和向下移动
    //bounds:前两个参数后期:就是(0,0)
    view.bounds = CGRectMake(10, 10, 120, 120);
    
    view.backgroundColor = [UIColor redColor];
    [self.view addSubview:view];
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
    view1.backgroundColor = [UIColor cyanColor];
    [view addSubview:view1];
    
    /*******UIView-变形属性**********/
    UIView *viewTransform = [[UIView alloc] initWithFrame:CGRectMake(100, 170, 100, 100)];
    viewTransform.backgroundColor = [UIColor orangeColor];
//    150  270
    //变形属性是以中心点为基准的
    //变形属性:transform
    //大小变形:CGAffineTransformMakeScale  width*sx   heigth*sy
    viewTransform.transform = CGAffineTransformMakeScale(2, 0.5);
    //角度变形:CGAffineTransformMakeRotation
    viewTransform.transform = CGAffineTransformMakeRotation(M_PI_4);

    //NSStringFromCGPoint  将CGPoint类型转成字符串类型
    NSLog(@"%@",NSStringFromCGPoint(viewTransform.center));
    
    [self.view addSubview:viewTransform];
    
    /********UIView-圆角设置***********/
    UIView *viewLayer = [[UIView alloc] initWithFrame:CGRectMake(110, 300, 100, 100)];
    viewLayer.backgroundColor = [UIColor magentaColor];
    
    //圆角设置:layer
    //圆角大小:cornerRadius   正方形边长的一半为圆
    viewLayer.layer.cornerRadius = 20;
    //边框设置:borderWidth
    viewLayer.layer.borderWidth = 5;
    //设置边框颜色:borderColor  默认黑色  [UIColor greenColor].CGColor
    viewLayer.layer.borderColor = [UIColor greenColor].CGColor;
    viewLayer.layer.borderColor = [[UIColor greenColor] CGColor];
    //是否切割子视图超出圆角的部分  :  YES:切割掉   默认NO:不切割
    //如果masksToBounds=YES  阴影效果出不来
    viewLayer.layer.masksToBounds = NO;
    
    //阴影
    //阴影的透明度:shadowOpacity  默认0.0
    viewLayer.layer.shadowOpacity = 1.0;
    //阴影的偏移量:shadowOffset
    viewLayer.layer.shadowOffset = CGSizeMake(50, 50);
    //阴影的颜色:shadowColor
    viewLayer.layer.shadowColor = [UIColor blueColor].CGColor;
    //阴影角度:shadowRadius   带有虚化的效果
    viewLayer.layer.shadowRadius = 30;
    
    [self.view addSubview:viewLayer];
    /*************UIView-层次关系***************/
    
    //添加视图:addSubview
    [self.view addSubview:view5];
    //移除视图:removeFromSuperview
//    [view5 removeFromSuperview];
    //将某个视图移动到最上面  bringSubviewToFront:
    [self.view bringSubviewToFront:view1];
    //将某个视图移动到最下面   sendSubviewToBack:
    [self.view sendSubviewToBack:view1];
    //将某一个视图移动到另一个视图的上面
    [self.view insertSubview:view1 aboveSubview:view3];
    //将某一个视图移动到另一个视图的下面
    [self.view insertSubview:view1 belowSubview:view2];
    //将某个视图放到指定的位置
//    [self.view insertSubview:view1 atIndex:3];
    //交换两个视图的位置
    [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:4];
    
    /***********UIView-响应关系***********/
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(70, 280, 160, 40)];
    view.backgroundColor = [UIColor blueColor];
    [self.view addSubview:view];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(80, 5, 160, 30);
    button.backgroundColor = [UIColor purpleColor];
    [button setTitle:@"button" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    /*
     超出父视图范围的部分不参与用户响应,只有在父视图之内的部分才会参与用户响应。
     */
    
    //UILabel
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 340, 280, 60)];
    label.backgroundColor = [UIColor redColor];
    //label 添加的按钮想要展示的话需要将clipsToBounds=YES
    //UIView的属性:是否切割多余视图的属性
    label.clipsToBounds = YES;
    //只有当clipsToBounds=YES时,label的圆角效果才能出来
    label.layer.cornerRadius = 20;
    
    //userInteractionEnabled  打开用户响应开关的属性   当为YES时:可以参与用户响应   只有UILabel和UIImageView默认NO
    /*
     只有父视图能够接受用户响应,它上面的子视图才能参与用户响应
     */
    label.userInteractionEnabled = YES;
    
    [self.view addSubview:label];
    //UIButton
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(40, 10, 200, 40);
    button1.backgroundColor = [UIColor grayColor];
    [button1 setTitle:@"button" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(buttonDown:) forControlEvents:UIControlEventTouchUpInside];
    [label addSubview:button1];
}

 

//博客园竟然也有这种功能,以前竟没发现...

posted @ 2015-11-22 17:17  向日夏  阅读(463)  评论(0编辑  收藏  举报