❀自我唠嗑-UI-UIView

/—————————-Window—————————-/
1.Window创建
Xcode5之前需要代码创建UIWindow对象

_window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    [_window setBackgroundColor:[UIColor colorWithRed:175/255.0 green:215/255.0 blue:237/255.0 alpha:1.]];
    [_window makeKeyAndVisible];

Xcode7开始,UIWindow对象创建后,必须指定根视图控制器(rootViewController属性)

//创建一个Windows,并使其铺满屏幕
    _window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    [_window setBackgroundColor:[UIColor colorWithRed:175/255.0 green:215/255.0 blue:237/255.0 alpha:1.]];
    //创建视图控制器
    ViewController *root = [[ViewController alloc] init];

    //设置windows的根视图控制器
    [_window setRootViewController:root];

    //绘制window,使其显示在屏幕上
    [_window makeKeyAndVisible];

/—————————-UIView—————————–/
iOS中几乎所有可视化控件都是UIView的子类,UIView响应该区域内发生的触摸事件,负责渲染区域内容等等,例如:管理矩形区域内的内容;处理矩形区域内各种事件;管理子视图;实现UIView动画;它的子类具有它的功能.总之…它很屌!

1.关于坐标系

struct CGRect {
    CGPoint origin;
    CGSize size;
};
以上明显看出,结构体中套了如下两个结构体
typedef struct CGRect CGRect;
/* Points. */
struct CGPoint {
    CGFloat x;
    CGFloat y;
};
typedef struct CGPoint CGPoint;

/* Sizes. */

struct CGSize {
    CGFloat width;
    CGFloat height;
};
typedef struct CGSize CGSize;

❤️iOS中设置矩形大小就是由CGRect类型来完成操作,利用CGRectMake()函数可以帮我们快速构造CGRect变量,另外,虽然坐标系界面像数学坐标系第四象限,但没有半毛钱关系,x,y都为正

2.UIView创建

//开辟空间并初始化视图(初始化时给出视图位置和大小)
UIView *view1 = [[UIView alloc] init];
    [view1 setFrame:CGRectMake(125, 50, 65, 65)];
    //设置UIView对象的属性
    [view1 setBackgroundColor:[UIColor colorWithRed:249/255.0 green:205/255.0 blue:173/255.0 alpha:1.0]];
    //将UIView对象添加到Window上显示
    [self.window addSubview:view1];
    //ARC模式下忽略以下
    [view1 release];

❤️frame决定了视图的大小和位置,基于它父视图的坐标系为参考

3.UIView常用属性和方法
这里写图片描述
这里写图片描述
这里写图片描述

4.关于bounds
一句话就是丫的霸道,生成自个左上角为原点坐标的坐标系,顶下规矩.它的子视图得以其左上角坐标定的大小为基准设置位置

附:❀色块铺屏小练习

_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [_window setBackgroundColor:[UIColor colorWithRed:222/255.0 green:89/255.0 blue:152/255.0 alpha:1]];
    ViewController *VC = [[ViewController alloc] init];
    [_window setRootViewController:VC];
    [_window makeKeyAndVisible];
    float x = [UIScreen mainScreen].bounds.size.width/5;
    float y = [UIScreen mainScreen].bounds.size.height/8;
    for (float i = 0; i < x * 5; i = i + x) {
        for (float j = 0; j < y * 8; j = j + y) {
            UIView *viewy = [[UIView alloc] initWithFrame:CGRectMake(i, j, x, y)];
            [viewy setBackgroundColor:[UIColor colorWithRed:arc4random() % 255/255. green:arc4random() % 255/255. blue:arc4random() % 255/255. alpha:1]];
            [self.window addSubview:viewy];
        }
    }

预览:
运行效果美美哒

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值