UIViewAPI
UI控件的直接或者间接父类
初始化方法
//其中frame是相对于父控件的位置
[[UIView alloc]initWithFrame];
center
控件的中心位置 CGPoint类型
其他通用方法
背景颜色
view.backgroundColor = [UIColor redColor]
文本
view.text = @"test"
相对于本身的大小
view.bounds CGReckMake类型,相当于在重设位置
形变
transform = label.transform = CGAffineTransformMakeRotation(M_PI_4);
父视图可以吧子视图超出的部分裁剪
view.clipsToBounds = YES;
是否可以隐藏
view.hidden = YES;
透明度
view.alpha = 1 //1 为不透明
是否可以与用户交互
view.userInteractionEnabled = YES 如果为NO的话,在这个视图上的子视图也不可以与用户交互
唯一标示符
view.tag = 1000 //最好设置大一点,以免与系统的冲突
//可以找到tag值为1000的值 然后前边用指定类型接收
UIButton * button2 = (id)[view viewWithTag:1000];
#####视图的层级关系
//提到最上边
[self.view bringSubviewToFront:view1];
//提到最后边
[self.view sendSubviewToBack:view2];
//把一个视图放到另一个输入上边
[self.view insertSubview:view2 aboveSubview:view1];
//数字越大,越靠前
[self.view insertSubview:view2 atIndex:1];
//取得一个视图上边所有的视图
NSArray * array = [self.view subviews];
//取得一个view的父视图
UIView * superView = view1.superview;
//交换两个子视图的位置
[view1 exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
//前者是不是后者的子视图 或者子视图的子视图 ,以此类推 返回bool值
BOOL isSubView = [view1 isDescendantOfView:self.view];
视图动画
//第一种系统动画方法
//在2秒的时间内完成如下操作
//把颜色从原来的变为绿色
//把frame改变为如下值
[UIView animateWithDuration:2.0 animations:^(void){
view.backgroundColor = [UIColor greenColor];
view.frame = CGRectMake(50, 100, 20, 10);
}];
//第二种系统动画方法
//在5秒的时间内完成如下操作
//改变frame
//改变alpha
//完成这些后初始化一个label
//设置label的背景颜色
//在这个view上添加一个label
[UIView animateWithDuration:5.0 animations:^(void){
view.frame = CGRectMake(100, 200, 200, 100);
view.alpha = 0.5;
}completion:^(BOOL finish){//动画结束之后做的事
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
label.backgroundColor = [UIColor blueColor];
[view addSubview:label];
}];
//除了这样写,还可以把block单独拿出来,然后在写
//这样更清晰一点
void(^begain)(void) = ^(void){
};
void(^finish)(BOOL) = ^(BOOL finish){
};
[UIView animateWithDuration:2.0 animations:begain completion:finish];
UI的layer层
//圆角矩形
label.layer.cornerRadius = 10;
//边框的粗细
label.layer.borderWidth = 3;
//边框的颜色
label.layer.borderColor = [UIColor redColor].CGColor;
//是否去除圆角矩形之外的东西
label.layer.masksToBounds = YES;
子视图根据父视图的大小改变自己的大小
//在动画的时候无效,自己点击可以实现父视图扩大的同时,子视图野跟着变大
view.autoresizesSubviews = YES;
label.autoresizingMask = UIViewAutoresizingFlexibleHeight;
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;