UI 学习简单总结(1)

来源:互联网 发布:ubuntu nodejs应用部署 编辑:程序博客网 时间:2024/05/09 00:37

    CGPoint,结构体, 用于存放一个点的坐标

   CGPoint point = CGPointMake(50, 100);

   NSLog(@"%.lf, %.lf", point.x, point.y);

   NSLog(@"%@", NSStringFromCGPoint(point));

   

    NSValue,用于把结构体类型 转化成 对象

   NSValue *value = [NSValue valueWithCGPoint:point];

    NSLog(@"%@",value);

   

    CGSize,结构体, 用于存放宽和高

   CGSize size = CGSizeMake(20, 30);

   NSLog(@"%.lf, %.lf", size.width, size.height);

   NSLog(@"%@", NSStringFromCGSize(size));

   

    CGRect,结构体, 用于描述一个矩形的位置

   

   CGRect rect = CGRectMake(50, 100, 20, 30);

   NSLog(@"%.lf, %.lf, %.lf, %.lf", rect.origin.x, rect.origin.y,rect.size.width, rect.size.height);

   NSLog(@"%@", NSStringFromCGRect(rect));

   

    UIScreen,屏幕类

    获取主屏幕, [UIScreen mainScreen]

    获取屏幕大小, [UIScreen mainScreen] bounds]

NSLog(@"%@",NSStringFromCGRect([[UIScreen mainScreen] bounds]));

 

    pt 和 px 的关系:“取决于设备

    iPhone 1, 3g,3gs          : 1pt = 1px  image.png

    iPhone 4, 4s,5, 5c, 5s, 6    : 1pt = 2px  image@2x.png

    iPhone6plus             : 1pt = 3px  image@3x.png

    屏幕大小,取决于设备:

    iPhone 1, 3g,3gs, 4, 4s      : 320 * 480 pt

    iPhone 5, 5c,5s            : 320* 560 pt

    iPhone 6                  : 375 * 667 pt

    iPhone6plus              : 414 * 736 pt

 

    创建window对象, window的大小和屏幕的大小保持一致

    self.window =[[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    设置背景的颜色

    UIColor, 颜色类

   self.window.backgroundColor = [UIColor whiteColor];

    成为主窗口, 并显示

    [self.windowmakeKeyAndVisible];

 

    创建一个视图对象

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

    设置属性

    设置背景颜色, 默认是clearColor(透明色)

   view.backgroundColor = [UIColor redColor];

    是否隐藏, 默认不隐藏

    view.hidden =NO;

   

    透明度, 范围[0, 1]

    view.alpha =0.5;

   

    向window上添加子视图

    [self.windowaddSubview:view];   

视图的层级关系:

    一个视图只能有一个父视图 superview

    一个视图可以有多个子视图 subview

   

    子视图的坐标以父视图的坐标系为基准

NSLog(@"%@", self.window);

   NSLog(@"%@", greenView.superview);

   NSLog(@"%@", greenView.subviews);

   

    超出父视图的部分是否切除, 默认不切

   greenView.clipsToBounds = NO;

 

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

   redView.backgroundColor = [UIColor redColor];

    [greenViewaddSubview:redView];

    [greenViewinsertSubview:redView atIndex:0];

   

    层级切换(父视图管理子视图)

    把子视图移到最前面

    [greenViewbringSubviewToFront:view9];

    把子视图放到最后面

    [greenView sendSubviewToBack:redView];

    交换两个子视图的位置

    [greenViewexchangeSubviewAtIndex:0 withSubviewAtIndex:1];

   

   移除子视图

    [redViewremoveFromSuperview];

   

    父视图

    添加子视图: add, insert

    改变子视图的位置:front, back, exchange

   

    frame:控制位置和大小

    center:控制位置(中心点)

    bounds:控制大小(以自己的左上角为原点)

   

    视图位置信息的属性

    frame, 子视图在父视图坐标系中的位置和大小

   NSLog(@"%@", NSStringFromCGRect(greenView.frame));

    center, 子视图的中心点在父视图坐标系中的位置

   NSLog(@"%@", NSStringFromCGPoint(greenView.center));

    bounds, 视图在自身坐标系中的位置和大小

   NSLog(@"%@", NSStringFromCGRect(greenView.bounds));

   

    fram改变,会影响center和bounds

    center改变,只影响frame,bounds不变

    bounds改变,只影响frame,center不变

    注:center.x = frame.x + frame.width / 2;

    center.y =frame.y + frame.heigth / 2;

    bounds修改会影响自身坐标系,会影响子视图的位置。

   

    注:view的frame必须整体修改

   greenView.frame = CGRectMake(200, 100, 100, 100);

   

    CGRect frame= greenView.frame;

   frame.origin.x += 100;

   greenView.frame = frame;

   

    NSLog(@"修改后:");

   NSLog(@"%@", NSStringFromCGRect(greenView.frame));

   NSLog(@"%@", NSStringFromCGPoint(greenView.center));

   NSLog(@"%@", NSStringFromCGRect(greenView.bounds));

 后边还有,稍后就会总结

 

0 0
原创粉丝点击