UIView简介

UIView简介

https://developer.apple.com/documentation/uikit/uiview?language=objc

官方解释:An object that manages the content for a rectangular area on the screen.
中文解释:UIView 是用于装载并展示各类控件的大容器,是iOS中所有UI控件的基类

常见属性

frame 位置和尺寸(以父控件的左上角为原点(0,0))
center 中点 (以父控件的左上角为原点(0,0))
bounds 位置和尺寸(以自己的左上角为原点 (0,0))
transform 形变属性(缩放,旋转)
backgroundColor 背景颜色
tag 标识(父控件可以根据这个标识找到对应的子控件,同一个父控件中的子控件不要一样)
hidden 设置是否要隐藏
alpha 透明度(0~1);
opaque 不透明度(0~1);
userInteractionEnabled 能否跟用户进行交互(YES 能交互)
superView 父控件
subviews 子控件
contentMode 内容显示的模式 拉伸自适应

[view viewWithTag:10];
[
    btn1 9
    btn2 10
    imageView 2 10
 ]

UIView 初始化实例

 UIView *view1 = [[UIView alloc] init];
 view1.frame = CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height/2);
 view1.backgroundColor = [UIColor lightGrayColor];
 view1.tag = 1000;
 
  //alpha属性设置view的透明度
 view1.alpha = 0.5;
 
 //超出部分自动隐藏
view1.clipsToBounds = YES;
[self.view addSubview:view1];

常见方法

1.addSubview
添加子控件,被添加到最上面(subviews中的最后面)

2.removeFromSuperview
从父控件中移除

3.viewWithTag:
父控件可以根据这个tag 标识找到对应的控件(遍历所有的子控件)

4.insertSubview:atIndex:
添加子控件到指定的位置

5.利用两个类方法来执行动画的两个方法
+(void) beginAnimations:(NSString *)animationID context:(void *)context;
/**..需要执行动画的代码..**/
+(void) commitAnimations;

6.利用blok 执行动画
/*
 duration 动画持续时间 
 animations 存放需林执行动画的代码
 completion 存放动画完毕后需要执行的操作代码
 */
+ (void) animateWithDuration:(NSTimeInterval) duration animations:(void (^)(void))animations completion:(void(^)) (BOOL finished) completion

sendSubviewToBack、bringSubviewToFront

sendSubviewToBack 父视图可以使用sendSubviewToBack方法将子视图放在最下层

UIView *view3 = [[UIView alloc] init];
 view3.frame = CGRectMake(45, 45, 240, 260);
 view3.backgroundColor = [UIColor blueColor];
 view3.tag = 1001;
 [view1 addSubview:view3];
    
 //父视图把某一子视图放在最下层
 [view1 sendSubviewToBack:view3];

bringSubviewToFront 父视图可以使用sendSubviewToBack方法将子视图放在最上层

UIView *view2 = [[UIView alloc] init];
view2.frame = CGRectMake(40, 40, 250, 250);
view2.backgroundColor = [UIColor blackColor];
view2.tag = 1001;
[view1 addSubview:view2];

 //父视图把某一子视图放在最上层
 [view1 bringSubviewToFront:view2];

猜你喜欢

转载自blog.csdn.net/guyindong/article/details/88649542