码迷,mamicode.com
首页 > 其他好文 > 详细

0515.View Hierarchy [UIKit]

时间:2014-05-15 12:13:20      阅读:430      评论:0      收藏:0      [点我收藏+]

标签:objective-c   hierarchy   uikit   uiview   

几个单词

 

Hierarchy[‘ha???k?]n.层级

思考:每个视图有一个父视图,有0个或者多个子视图

 

Manipulation[m?,n?pj?‘le??(?)n] n.操纵;操作

 

Descendant[d?‘send(?)nt] n. 后裔;子孙

 


UIWindow

 

Feature

1UIWindow set up by default in Xcodetemplate project,and contains the entire view hierarchy

2UIWindow just a view,and adds someadditional to top level view

3Views live inside of a window

思考:UIWIndow位于toplevel,启动APP的时候首先启动,其他所有在上面的View依次渲染出现



Principle


UIView的属性 

@property(nonatomic,readonly) UIView *superview;
@property(nonatomic,readonly,copy) NSArray *subviews;
@property(nonatomic,readonly) UIWindow *window;

思考:每一个View都有一个superview,有一个对应的subviews(注意是NSArraay),有一个window.所有对子视图的操作,其实本质上来讲也就是对数组的操作。视图的增删改,对应的就是数组的增删改。当然也有视图的回调方法用于跟踪视图改变。



ViewManipulation

 

1、add or remove in IB or using UIVIew methods

  • (void)addSubview:(UIView *)view;
  • (void)removeFromSuperview;

示例:

    // UIWindow
    self.window.backgroundColor = [UIColor darkGrayColor];
    // view_blue
    UIView *view_a = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    view_a.backgroundColor = [UIColor blueColor];
    // view_yellow
    UIView *view_b = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
    view_b.backgroundColor = [UIColor yellowColor];
   
    // 依次添加view_blue、view_yellow到UIWindow
    [self.window addSubview:view_blue];
    [self.window addSubview:view_yellow];

bubuko.com,布布扣

 

思考:上图很清楚的看出来,视图的层次关系是:UIWindow -> View_blue -> View_yellow

 

此时如果更改添加顺序,代码和效果图如下:

// 添加view_yellow、view_blue到UIWindow [self.window addSubview:view_yellow]; [self.window addSubview:view_blue]; [self.window addSubview:view_yellow]; [self.window addSubview:view_blue];

     bubuko.com,布布扣


思考:很明显现在的视图层次是:UIWindow -> View_yellow-> View_blue,此时self.window.subviews打印的结果是:

views:(

    "<UIView:0x8f1ecc0; frame = (50 50; 100 100); layer = <CALayer:0x8f1ed20>>",

    "<UIView:0x8f13330; frame = (0 0; 100 100); layer = <CALayer: 0x8f1d800>>"

)

 

3、otherViewmanipulationmethods

- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index; - (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview; - (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview; - (void)bringSubviewToFront:(UIView *)view; - (void)sendSubviewToBack:(UIView *)view; - (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2;

思考:每一个View的子视图其实就是一个数组,对View的子视图的操作也就是对当前view的子视图数组进行操作。常见的操作无非尾部增加、特定位置增加、删除、调整在数组中的位置、调换2个数组元素的位置而已。

 

示例:

    

//UIWindow
    self.window.backgroundColor = [UIColor darkGrayColor];
    //view_blue
    UIView *view_blue = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    view_blue.backgroundColor = [UIColor blueColor];
    //view_yellow
    UIView *view_yellow =[[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
    view_yellow.backgroundColor = [UIColor yellowColor];
   
    //view_red
    UIView *view_red = [[UIView alloc]initWithFrame:CGRectMake(25, 25, 100, 100)];
    view_red.backgroundColor = [UIColor redColor];
   
    // 添加view_yellow、view_blue到UIWindow
    [self.window addSubview:view_yellow];
    [self.window addSubview:view_blue];
    [self.window insertSubview:view_red atIndex:0];
   
    NSLog(@"views:%@",self.window.subviews);

bubuko.com,布布扣

思考:上面的[self.windowinsertSubview:view_redatIndex:0];这句话其实就是把红色的view添加到UIWindow子视图数组的第1个位置,所以位于blue、yellow的下层

 

如果在这句话[self.windowinsertSubview:view_redatIndex:0];后面加上这句话

    [self.windowinsertSubview:view_redatIndex:0];那么产生的效果如下:


bubuko.com,布布扣

思考:其实本质都是对UIWIndow子视图数组的操作

 

4、Tracks theadditions and removals of subviews

 

- (void)didAddSubview:(UIView *)subview;
- (void)willRemoveSubview:(UIView *)subview;
 
- (void)willMoveToSuperview:(UIView *)newSuperview;
- (void)didMoveToSuperview;
- (void)willMoveToWindow:(UIWindow *)newWindow;
- (void)didMoveToWindow;

 

思考:视图回调主要是用于跟踪视图的相关改变

 

5、Set view tag and search view

 

myView.tag = 1011;

UILabel *label =(UILablel *)[self.view viewWithTag:1011];

 

6、isDescendantOfView

 

- (BOOL)isDescendantOfView:(UIView *)view;

思考:主要用于判断的一个View是不是另一个View的子subView

 

0515.View Hierarchy [UIKit],布布扣,bubuko.com

0515.View Hierarchy [UIKit]

标签:objective-c   hierarchy   uikit   uiview   

原文地址:http://blog.csdn.net/paulery2012/article/details/25868885

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!