Lesson01UIView 用户界面

上课代码
<span style="font-size:18px;">    //UI: User Interface, 用户界面
    //UIWindow, 窗口类, 所有的控件必须放到window上才能显示, 一个iOS应用至少要有一个窗口, 继承于UIView
    //iOS手机中的坐标系, 不同于数学中笛卡尔坐标系, 原点在左上角, X轴: 向右越来越大, y轴: 向下越来越大
    
    //与坐标系相关的数据类型
    
    //CGPoint, 结构体, 用于存放一个点的坐标
    CGPoint point = CGPointMake(10, 100);
    NSLog(@"%lf, %.2lf",point.x, point.y);
    NSLog(@"%@",NSStringFromCGPoint(point));
    
//  CGSize, 结构体, 用于存放矩形的宽和高
    CGSize size = CGSizeMake(150, 200);
    NSLog(@"%lf, %.2lf",size.width, size.height);
    NSLog(@"%@",NSStringFromCGSize(size));
    
    //CGRect, 结构体, 用于存放一个矩形的位置和大小
    CGRect rect = CGRectMake(10, 100, 150, 200);
    NSLog(@"%.2lf, %.2lf, %.2lf, %.2lf",rect.origin.x, rect.origin.y, rect.size.width,rect.size.height);
    NSLog(@"%@",NSStringFromCGRect(rect));
    
    // [[UIScreen mainScreen] bounds], 主屏幕的大小
    //[UIScreen mainScreen], 获取到主屏幕
    //UIScreen, 屏幕类
    
    NSLog(@"%@",NSStringFromCGRect([[UIScreen mainScreen] bounds]));
    //屏幕大小(单位:pt, point)
    //1, 3g, 3gs, 4, 4s: [320 * 480]
    //5, 5s, 5c: [320 * 568]
    //6, 6s: [375 * 667]
    //6Plus, 6s Plus: [414 * 736]
    
    //创建一个和屏幕一样大小的window对象
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    
    //UIColor, 颜色类, 继承于NSObject, 用于展示一种颜色
    //RBGA,(红,蓝,绿,不透明度) 0~1
    UIColor *color = [UIColor colorWithRed:1.000 green:0.649 blue:0.533 alpha:1.000]; //  OMColorSense插件,设置颜色
    
    
    //设置背景颜色
    self.window.backgroundColor = [UIColor yellowColor];
    //让当前的窗口成为主窗口, 并显示
    [self.window makeKeyAndVisible];
    //ARC->MRC
    //1.gar
    //2.strong
    //3.重写dealloc
    //4.window, autorelease
    
    //UIView, 继承于UIResponder, 一个矩形区域, 所有控件都是继承于UIView, 用户在手机上能够看到的都是UIView或者UIview的子类
    //创建一个视图的步骤:
    //1.创建视图, 并给定其位置和大小
    UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 100, 200)];
    
    //2.设置视图属性
    //颜色
    aView.backgroundColor = [UIColor yellowColor];
    //是否隐藏
    aView.hidden = YES;
    //不透明度
    aView.alpha = 0.5;
    
    //3.添加到window上
    //所有的视图都要放到window上才能显示
    [self.window addSubview:aView];
    
    //4.释放视图
    [aView release];
    
//    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
//    redView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
//    redView.hidden = NO;
//    redView.alpha = 1;
//    [self.window addSubview:redView];
//    [redView release];
//    
//    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//    blueView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:1];
//    blueView.hidden = NO;
//    blueView.alpha = 1;
//    [self.window addSubview:blueView];
//    [blueView release];
//
//    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(150, 150, 100, 100)];
//    greenView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:1];
//    greenView.hidden = NO;
//    greenView.alpha = 1;
//    [self.window addSubview:greenView];
//    [greenView release];
    
    
//    CGFloat p = 0;
//    while (p <= 375) {
//        UIView *radomView = [[UIView alloc] initWithFrame:CGRectMake(p, p, 100, 100)];
//
//        radomView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.0];
//        radomView.hidden = NO;
//        [self.window addSubview:radomView];
//        [radomView release];
//        p += 50;
//
//    }
//    for (NSInteger i = 0; i < 30; i++) {
//        CGFloat x = arc4random() % (375 - 10 + 1) / 1.0;
//        CGFloat y = arc4random() % (667 - 10 + 1) / 1.0;
//        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, 10, 100)];
//        view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255. green:arc4random() % 256 / 255. blue:arc4random() % 256 / 255. alpha:1.0];
//    }

    
    //视图的层级关系
    //1.越晚添加的视图,显示在最前面
    //2.一个视图只能有一个字视图, 但是可以有多个子视图
    //3.一个视图的位置, 是相对于他的父视图计算的
    
    
    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 300, 300)];
    redView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
    redView.hidden = NO;
    [self.window addSubview:redView];
    [redView release];
    
    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    blueView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:1];
    //超出父视图部分是否切除, 默认NO
    blueView.clipsToBounds = YES;
    blueView.hidden = NO;
    //redView为blueView的父视图
    [redView addSubview:blueView];
    [blueView release];
    
    UIView *greenView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
    greenView.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:1];
    greenView.hidden = NO;
    //超出父视图的部分被无情切除
    [blueView addSubview:greenView];
    [greenView release];
    
    
    NSLog(@"%@",redView.superview);
    NSLog(@"%@", redView.subviews);

    //center, 基于父视图的坐标系, 视图中心点在父视图坐标系中的位置
    NSLog(@"%@", NSStringFromCGPoint(redView.center));//转成字符串打印
    NSLog(@"%@", NSStringFromCGPoint(blueView.center));//基于父视图的坐标系,转成字符串打印
    
    //fram, 基于父视图坐标系, 视图在父视图坐标系中的位置和大小
    NSLog(@"%@",NSStringFromCGRect(redView.frame));//基于父视图坐标系
    NSLog(@"%@",NSStringFromCGRect(blueView.frame));

    //bounds, 基于自身坐标系, 视图在自身坐标系的位置和大小, 默认值{0,0,w,h}
    NSLog(@"%@",NSStringFromCGRect(redView.bounds));
    
    //UIView属性
    //1.backgroundColor
    //2.hidden
    //3.alpha
    //4.superview
    //5.subviews
    //6.
    //7.center
    //8.frame
    //9.bounds</span>
    


你可能感兴趣的:(UIView,控件,界面)