UI 第一步 UIView

刚学习UI,就像在孤岛漂流了两年,突然回到人类社会,总感觉那么新奇好玩,终于可以不用对着无趣的控制台了

其实一个手机肯定有壁纸,也就是父视图,其他的图标也是叠在父视图上面


#import "AppDelegate.h"
#import "ViewController.h"//首先先引入ViewController.h头文件
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

//创建一个window,并使其铺满屏幕
 _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

//给父视图_window上shai
_window setBackgroundColor:[UIColor buleColor];

//创建一个视图控制器
ViewController *root = [[ViewController alloc] init];

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

//绘制window
[_window makeKeyAndVisible];

父视图创建好后就可以愉快的添加子视图了

创建子视图的第一步,申请内存空间并初始化

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

第二步就是设置子视图的坐标和大小了

view setFrame:CGRectMake(0,0,200,200)];

第三步设置子视图的各种属性

//设置子视图的背景shai
[view setBackgroundColor:[UIColor redColor]];

第四步,把子视图添加到父视图上

[self.window addSubview:view];

一个父视图上面叠个子视图就弄好了

关于子视图还有很多方法可以通过查API和SDK查看

//在指定的index处插入子视图
    UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(140, 140, 200, 200)];
    [view4 setBackgroundColor:[UIColor cyanColor]];
    [self.window insertSubview:view4 atIndex:2];

    
    //指定的视图上面添加子视图
    UIView *view5=  [[UIView alloc] initWithFrame:CGRectMake(160, 160, 200, 200)];
    [view5 setBackgroundColor:[UIColor magentaColor]];
    [self.window insertSubview:view5 aboveSubview:view3];
    
    //在指定的视图下面添加子视图
    UIView *view6 = [[UIView alloc] initWithFrame:CGRectMake(6, 6, 120, 120)];
    [view6 setBackgroundColor:[UIColor grayColor]];
    [self.window insertSubview:view6 belowSubview:view3];
    
    //把子视图挪到最上面,层级变化了,但xy位置并不会改变
    [self.window bringSubviewToFront:view2];
    
    //把子视图挪到最下面,层级变化了,但xy位置并不会改变
    [self.window sendSubviewToBack:view6];
    
    //交换两个指定子视图的位置
    [self.window exchangeSubviewAtIndex:3 withSubviewAtIndex:5];
    
    //移除子视图,再想显示只能重新添加
    [view2 removeFromSuperview];


今天学到的马赛克

还是先创建父视图

    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    [_window setBackgroundColor:[UIColor colorWithRed:28/255.0 green:173/255.0 blue:253/255.0 alpha:1]];
 
    ViewController *root = [[ViewController alloc] init];
    [_window setRootViewController:root];
    [_window makeKeyAndVisible];

然后就可以在父视图上面画马赛克了

    
    //外循环铺满屏幕的长度
    for (int i = 0; i < 200; i++) {
        //内循环铺满屏幕的宽度
        for (int j = 0; j < 120; j++) {
            //根据屏幕的长除于几就等于屏幕的几分之1长,宽度同理
            UIView *v5 = [[UIView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width / 120)*j, ([UIScreen mainScreen].bounds.size.height / 200)*i, [UIScreen mainScreen].bounds.size.width / 120, [UIScreen mainScreen].bounds.size.height / 200)];
            //随机上shai
            [v5 setBackgroundColor:[UIColor colorWithRed:arc4random() % 225/255.0 green:arc4random() % 255/255.0 blue:arc4random() % 255/255.0 alpha:1]];
            //把子视图添加到父视图
            [self.window addSubview:v5];
        }}

万恶的马赛克就出来了


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值