UIView的使用

uiview 是视图,即显示在手机屏幕,能看得到的东西。

一切显示的视图控件都是uiview的子类,uiview有的属性,其子类也有。

视图在手机屏幕上的显示,在满足几个条件:

必须实例化

设置frame,即坐标(x坐标、y坐标),大小(长、宽),注意frame是针对其所在的父视图来设置的

添加到父视图,即实现 addSubview 方法

注意其隐藏属性hidden设置为NO(默认为NO,即可见),以及透明度alpha设置为1.0(默认为1.0,即不透明)


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 实例化视图  
  2. UIView *view01 = [[UIView alloc] init];  
  3. // 设置背景颜色  
  4. view01.backgroundColor = [UIColor redColor];  
  5. // 坐标大小设置  
  6. view01.frame = CGRectMake(10.050.0100.0100.0);  
  7. // 隐藏属性,NO为显示,YES为隐藏,默认为NO  
  8. view01.hidden = NO;  
  9. // 透明度,取值范围0.0~1.0,默认为1.0  
  10. view01.alpha = 1.0;  
  11. // 视图tag值,相当房号,注意:tag值的设置通常都设置成大于1000,避免与系统控件的tag值重复,造成冲突  
  12. view01.tag = 1000;  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 添加到父视图,即self.view父视图,添加到父视图才可见,否则不可见  
  2. [self.view addSubview:view01];  
  3. // 从父视图删除,删除后不可见  
  4. //    [view01 removeFromSuperview];  
  5.       
  6. // layer图层属性  
  7. // 圆角属性  
  8. view01.layer.cornerRadius = 10.0;  
  9. // 边框大小  
  10. view01.layer.borderWidth = 5.0;  
  11. // 边框颜色  
  12. view01.layer.borderColor = [UIColor yellowColor].CGColor;  
  13. // 图层遮罩(避免有时候圆角属性设置不成功)  
  14. view01.layer.masksToBounds = YES;  


[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 其他属性  
  2. // center中心坐标  
  3. view01.center = CGPointMake(self.view.center.x, view01.center.y); // 水平居中  
  4. //    view01.center = CGPointMake(view01.center.x, self.view.center.y); // 垂直居中  
  5. //    view01.center = self.view.center; // 父视图的正中间  
  6. // superview父视图  
  7. UIView *superView = view01.superview;  
  8. superView.backgroundColor = [UIColor brownColor];  
  9. NSLog(@"superView %@", superView);  
  10. // bounds大小,视图的大小,忽略坐标,即相对于视图自己来设置的  
  11. CGRect bounds = view01.bounds;  
  12. NSLog(@"bounds %@", NSStringFromCGRect(bounds));  
  13. // 子视图数组  
  14. NSArray *viewArray = self.view.subviews;  
  15. NSLog(@"viewArray %@", viewArray);  
  16.       
  17. UIView *view02 = [[UIView alloc] initWithFrame:CGRectMake(20.0200.0200.0200.0)];  
  18. view02.backgroundColor = [UIColor yellowColor];  
  19. [self.view addSubview:view02];  
  20. view02.tag = 1001;  
  21.       
  22. UIView *view03 = [[UIView alloc] initWithFrame:CGRectMake(10.010.060.060.0)];  
  23. view03.backgroundColor = [UIColor greenColor];  
  24. [view02 addSubview:view03];  
  25. view03.tag = 1003;  
  26.       
  27. UIView *view04 = [[UIView alloc] initWithFrame:CGRectMake(10.010.080.080.0)];  
  28. view04.backgroundColor = [UIColor purpleColor];  
  29. [view02 addSubview:view04];  
  30.       
  31. // 改变两个视图在父视图的位置  
  32. [view02 exchangeSubviewAtIndex:0 withSubviewAtIndex:1];  
  33.       
  34. UIView *view05 = [[UIView alloc] initWithFrame:CGRectMake(20.020.0100.0100.0)];  
  35. view05.backgroundColor = [UIColor redColor];  
  36. // 插入子视图到指定视图之上  
  37. [view02 insertSubview:view05 aboveSubview:view04];  
  38. // 把指定的子视图放在父视图的最后一层  
  39. [view02 sendSubviewToBack:view05];  
  40. // 把指定的子视图放在父视图的最前面  
  41. [view02 bringSubviewToFront:view05];  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // 简单动画  
  2. // 方法1  
  3. [UIView beginAnimations:@"view001" context:nil]; // 动画开始标识  
  4. [UIView setAnimationDuration:3.0]; // 动画时间  
  5. view01.frame = CGRectMake(10.0, CGRectGetHeight(self.view.bounds) - 50.0100.0100.0); // 动画效果  
  6. [UIView commitAnimations]; // 开始动画  
  7. // 方法2  
  8. [UIView animateWithDuration:3.0 animations:^{  
  9.         view01.frame = CGRectMake(10.0self.view.center.y100.0100.0); // 动画效果  
  10. }];  


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值