//
// UIViewViewController.m
// AppUI组件学习
//
// Created by 麦子 on 15/6/16.
// Copyright (c) 2015年 麦子. All rights reserved.
//
#import "UIViewViewController.h"
@interface UIViewViewController (){
UIView *myview4;
}
@end
/***
UIView 就是一个容器,就像栈一样,一个有顺序的容器,他对层的操作会导致这个层的数组的下标这样的顺序有变动
**/
@implementation UIViewViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self createView:self.view];
}
- (void)createView:(id)uiView{
UIView *view = (UIView *)uiView;
UIView *myView = [[UIView alloc] init];
myView.frame = CGRectMake(10, 80, 300, 100);
myView.backgroundColor = [UIColor yellowColor];
// 自动裁减,超过父视图的地方,会自动裁掉
myView.clipsToBounds = YES;
CGFloat xCount = myView.frame.origin.x; // 相对于父类的左边上边的坐标
CGFloat yCount = myView.frame.origin.y;
CGFloat widthCount = myView.frame.size.width;
CGFloat heightCount = myView.frame.size.height;
NSLog(@"x-%f,y-%f,width-%f,height-%f",xCount,yCount,widthCount,heightCount);
// 获取中心店
CGPoint point = myView.center;
NSLog(@"x-%f,y-%f",point.x,point.y);
// 获取边框大小
CGRect rect = myView.bounds; // x y 永远是 0
NSLog(@"x-%f,y-%f,width-%f,height-%f",rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
// 设置视图标签,这个属于唯一标示
myView.tag = 1;
// 设置内容模式
myView.contentMode = UIViewContentModeLeft; // 这个可以在ImageView中,可以设置图片的放的位置
// view 一个容器,这个容器里的数据都可以通过方法来获取到
UIView *myView2 = [[UIView alloc] init];
myView2.frame = CGRectMake(10, 20, 200, 50);
myView2.backgroundColor = [UIColor purpleColor];
[myView addSubview:myView2];
UIView *myView3 = [[UIView alloc] init];
myView3.frame = CGRectMake(10, 80, 200, 100);
myView3.backgroundColor = [UIColor orangeColor];
// 设置通明度,
myView3.alpha = 0.1;
[myView addSubview:myView3];
// 得到父类视图
UIView *superView = [myView2 superview];
superView.backgroundColor = [UIColor blueColor];
// 获取所有子视图
NSArray *array = superView.subviews;
for (UIView *myView in array) {
myView.backgroundColor = [UIColor brownColor];
}
// 下标获取
UIView *firstView = [array objectAtIndex:0];
firstView.backgroundColor = [UIColor darkGrayColor];
// 设置自动布局
myview4 = [[UIView alloc] init];
myview4.frame = CGRectMake(10, 200, 300, 300);
myview4.backgroundColor = [UIColor yellowColor];
// 允许子类对象自动布局
myview4.autoresizesSubviews = YES;
UIView *myview5 = [[UIView alloc] init];
myview5.frame = CGRectMake(10, 20, 250, 250);
myview5.backgroundColor = [UIColor orangeColor];
// 设置子视图布局方式-------宽度的改变会和父类视图一致/高度, 这有多种方式
myview5.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
[myview4 addSubview:myview5];
[view addSubview:myview4];
// 层操作 ---- 类似于栈区的顺序
UIView *myView6 = [[UIView alloc] init];
myView6.frame = CGRectMake(10, 550, 50, 50);
myView6.backgroundColor = [UIColor purpleColor];
UIView *myView7 = [[UIView alloc] init];
myView7.frame = CGRectMake(30, 580, 100, 100);
myView7.backgroundColor = [UIColor redColor];
[view addSubview:myView6];
[view addSubview:myView7];
[view addSubview:myView];
// 将某一个层放在最下面
// [view sendSubviewToBack:myView7];
// 将某一层放在最上层
// [view bringSubviewToFront:myView7];
//插入一个view到指定的层
UIView *myView8 = [[UIView alloc] init];
myView8.frame = CGRectMake(50, 580, 150, 150);
myView8.backgroundColor = [UIColor blueColor];
// 插入到那一层
// [view insertSubview:myView8 atIndex:1];
// [view insertSubview:myView8 aboveSubview:myView6];// 上面 还有下面
[view exchangeSubviewAtIndex:1 withSubviewAtIndex:0]; // 交换层
// 这里自动就会调用了,不需要放入到主线程中,
// [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(viewChange:) userInfo:nil repeats:YES];
}
- (void)viewChange:(NSTimer *)timer{
myview4.frame = CGRectMake(10, 200, myview4.frame.size.width+5, myview4.frame.size.height+5);
}
@end
UIView
最新推荐文章于 2022-05-16 11:30:43 发布