//通过xib方式创建视图对象
NSBundle *bundle = [NSBundlemainBundle];
NSArray *arr = [bundleloadNibNamed:@"myView"owner:selfoptions:nil];
UIView *myView = [arrobjectAtIndex:0];
//代码创建
CGRect viewRect =CGRectMake(0,0,100,100);
UIView *myView = [[UIView alloc] initWithFrame:viewRect];
1.UIView 层次结构可以理解为“视图树”----view hierarchy。
2.一个视图就是一个容器,当一个视图包含其他视图的时候,两个视图之间就建立了一个父子关系。被包含的视图被称为“子视图(subview)”,包含的视图称为“父视图或超视图(superview)”。
3.从视觉上看,子视图隐藏了父视图的内容,设置透明属性可以看到父视图的内容。
4.每一个父视图都有一个有序的数组存储着它的子视图,存储的顺序就会影响到每个子视图的显示效果,比如:两个兄弟视图重叠在一起,后来被加入的视图就会出现在另外的上面。
5.一个视图可以嵌入多个subview,但是只能有一个superview。
常用方法:
- (void)addSubview:(UIView *)view;//添加子视图
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview;//视图插入指定视图之下
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview;//视图插入指定视图之上
- (void)bringSubviewToFront:(UIView *)view;//把视图移动到最顶层
- (void)sendSubviewToBack:(UIView *)view;//把视图移动到最底层
- (void)removeFromSuperview;//把视图从父视图中移除
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index;//视图插入到指定索引位置
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;//把两个索引对应的视图调换位置
6.UIView类中有一个tag属性,通过这个tag属性可以标示一个视图对象(整数)。
tag值不设的话默认为0
获取的方法:
UIView *view = [[UIViewalloc]initWithFrame:CGRectMake(60,100,200,100)];
[view setBackgroundColor:[UIColorredColor]];
//设置tag
view.tag =100;
[self.windowaddSubview:view];
//通过tag值查找view
UIView *myView = [self.windowviewWithTag:100];
7.UIView常用属性
alpha//透明度 默认是1(不透明)
backgroundColor
subViews//子视图集合
hidden//是否隐藏
tag//标签值
superview//父视图
multipleTouchEnabled//是否开启多点触摸
userInteractionEnabled//是否响应触摸事件
8.view的简单动画
//放大缩小
view.transform =CGAffineTransformScale(view.transform,0.8, 0.8);
//旋转
view.transform =CGAffineTransformRotate(view.transform,0.2);
//平移view.transform =CGAffineTransformTranslate(view.transform,50, 50);