UIView的基本应用

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
#if 0
  
    //UI-view 视图,基础单元(UI的基类)
    UIView * view1 = [[UIView alloc]init];
    //1.位置和大小 2.加载在父视图中
    //320x480px  状态栏 20px
    //位置大小
    //view1.frame=self.window.bounds;整个屏幕
    view1.frame = CGRectMake(10, 30, 300, 440);
    //设置背景颜色
    view1.backgroundColor = [UIColor blueColor];
    [self.view addSubview:view1];
    
    //同一个父视图中后加入的控件盖在上面
    UIView * view2 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
    view2.backgroundColor = [UIColor redColor];
    [self.view addSubview:view2];
    
    //坐标是相对父视图来进行设置的,遮挡关系是根据最大的父视图进行判定,
    //再逐一判定每个子视图的子视图
    UIView * view3 = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
    view3.backgroundColor = [UIColor orangeColor];
    [view1 addSubview:view3];
    
    //调整遮挡关系
    //1.将某一个子视图放在最上层
    [self.view bringSubviewToFront:view1];
    //2.将某一个子视图放在最下层
    [self.view sendSubviewToBack:view1];
    //3.交换两个层的视图
    [self.view exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    //4.将子视图加到某一个子视图下层
    UIView * view4 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 50, 50)];
    view4.backgroundColor = [UIColor cyanColor];
    [self.view insertSubview:view4 belowSubview:view2];
    //5.将子视图加入到某个子视图上方
    UIView * view5 = [[UIView alloc]initWithFrame:CGRectMake(5, 10, 100, 30)];
    view5.backgroundColor = [UIColor brownColor];
    [self.view insertSubview:view5 aboveSubview:view1];
    //6.将子视图加入到特定层
    UIView * view6 = [[UIView alloc]initWithFrame:CGRectMake(20, 20, 50, 50)];
    view6.backgroundColor = [UIColor whiteColor];
    [self.view insertSubview:view6 atIndex:4];
    
    NSLog(@"%f %f %f %f",view1.frame.origin.x,view1.frame.origin.y,view1.frame.size.width,view1.frame.size.height);
    
    NSLog(@"%f %f %f %f",view1.bounds.origin.x,view1.bounds.origin.y,view1.bounds.size.width,view1.bounds.size.height);//只能打印宽高
    
    //center
    NSLog(@"%f %f",view1.center.x,view1.center.y);
    
    //获得父视图
    UIView * superView = view1.superview;
    NSLog(@"%@",superView);
    
    //获得子视图(多个)
    NSArray * subViewsaArray = view1.subviews;
    NSLog(@"%@",subViewsaArray);
    
    //tag - 标识
    view1.tag = 1001;
    //通过tag找到子视图
    //UIWindow * tagView = [_window viewWithTag:1001];
    //tagView.backgroundColor = [UIColor lightGrayColor];
    
    /*
     < iOS6 UIView 自适应来做屏幕适配
     iOS6~iOS8 AutoLayout
     iOS8> AutoLayout+ SizeClass
     */
    
#endif
    UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(110, 200, 100, 100)];
    backView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:backView];
    
    backView.tag = 1002;
    //准许子视图跟随改变
    backView.autoresizesSubviews = YES;
    
    UIView * topView = [[UIView alloc]initWithFrame:CGRectMake(25, 25, 50, 50)];
    topView.backgroundColor = [UIColor greenColor];
    //2.设置子视图跟随方式
    topView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin;//多种变化方式用|
    [backView addSubview:topView];
    
    //定时器
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeTick) userInfo:nil repeats:YES];

    
    //剪裁超出俯视图范围的子视图
//    view1.clipsToBounds = YES;
    

    // Do any additional setup after loading the view, typically from a nib.
}

- (void)timeTick {
    UIView *backView = [self.view viewWithTag:1002];
    backView.frame = CGRectMake(backView.frame.origin.x-2, backView.frame.origin.y-2, backView.frame.size.width+4, backView.frame.size.height+4);
    
}

你可能感兴趣的:(对象,属性,UIView,ios开发,原理使用)