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

视图的层次结构

时间:2014-10-28 21:48:35      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:bounds   center   视图的层次结构   未读信息个数   

AppDelegate.m

#import "AppDelegate.h"
#import "TestView.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    <span style="color:#ff0000;">//设置未读信息数</span>
//    application.applicationIconBadgeNumber = 9;
    
    /*______________________________frame、bounds和center_____________________________________*/
    //坐标系的零点是在状态栏上面
    //创建视图
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(50, 20, 90, 90)];
    //设置视图的颜色
    view.backgroundColor = [UIColor redColor];
    
    //以其父视图的起点为原点
    view.frame = CGRectMake(0, 20, 320, 40);    //center: 160  40
    
    //错误,  <span style="color:#ff0000;">bounds的x和y必须为零,它可以改变设置大小,不可以改变位置</span>
//    view.bounds = CGRectMake(10, 20, 90, 90);
    
    //<span style="color:#ff0000;">center是用来表示视图的中心位置的,可以改变视图的位置,不可以改变视图的大小</span>
//    view.center = CGPointMake(160, 120);
    
    CGRect bounds = view.bounds;
    //NSStringFromCGRect将结构体类型的CGRect转换成字符串
    NSString *str = NSStringFromCGRect(bounds);
    NSLog(@"str:%@",str);
    
    //添加到window上显示
    [self.window addSubview:view];
    [view release];

    /*__________________________视图的层次结构___________________________*/
    
    //创建父视图
    UIView *superView = [[UIView alloc] initWithFrame:CGRectMake(0, 80, 320, 300)];
    superView.backgroundColor = [UIColor blackColor];
    //添加到window上面显示
    [self.window addSubview:superView];//retain
    [superView release];
    
    //给superView添加子视图
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(10, 20, 300, 200)];
    view1.backgroundColor = [UIColor greenColor];
    //添加到supView上面显示
    [superView addSubview:view1];
    [view1 release];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(20, 40, 300, 160)];
    view2.backgroundColor = [UIColor orangeColor];
    //添加到supView上显示
//    [superView addSubview:view2];
    
    //将视图插入到superView子视图的下面,索引为0
    [superView insertSubview:view2 atIndex:0];
    
    //将view2作为supView的子视图插入到view1的上面
    [superView insertSubview:view2 aboveSubview:view1];
    
    //将view2作为supView的子视图插入到view1的下面
    [superView insertSubview:view2 belowSubview:view1];
    
    [view2 release];
    
    //将view2作为supView的子视图移动到最上面
    [superView bringSubviewToFront:view2];
    
    //将view2作为supView的子视图移动到最下面
    [superView sendSubviewToBack:view2];
    
    <span style="color:#ff6600;">//交换superView两个子视图的位置</span>
    [superView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    
    <span style="color:#ff0000;">//取得当前视图的父视图view1.superview;</span>
    NSLog(@"%@",view1.superview);
    
    //如果想判断一个视图是否在显示,可以判断view.superview是否存在
    
   <span style="color:#ff0000;"> //将view1从父视图上移除</span>
    if (view1.superview != nil) {
        [view1 removeFromSuperview];
    }
    //注意:
//    removeFromSuperview只是将视图从父视图上面移除,但是不是销毁,还是在内存中存在的
    NSLog(@"view1:%@",view1);
    
    
    /*__________________________UIView的常用属性___________________________*/
    
    //1.<span style="color:#ff0000;">透明度</span>,范围是 [0,1]
    //注意:如果你设置父视图的透明度,子视图的这个属性也会跟着变化
//    superView.alpha = .5;
//    view2.alpha = .5;
    
    //2.背景颜色
//    clearColor是透明的
    superView.backgroundColor = [UIColor greenColor];
    //用过三色值设置颜色
//    superView.backgroundColor = [UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:<#(CGFloat)#>]
    
    //3.<span style="color:#ff0000;">subviews得到视图的所有子视图</span>
    NSArray *views = superView.subviews;
    for (UIView *view in views) {
        view.backgroundColor = [UIColor darkGrayColor];
    }
    
    /<span style="color:#ff0000;">/4.hidden 隐藏</span>
//    view2.hidden = YES;
    
    //5.superview得到当前视图的父视图
    NSLog(@"supView:%@",superView);
    NSLog(@"test:%@",view2.superview);
    
    //6<span style="color:#ff0000;">.tag:视图的标签值</span>
    /<span style="color:#ff0000;">/如果想通过tag得到视图,tag设置大于等于100</span>
    
    /*
     注意点:1:在同一个父视图上面,两个子视图不能设置相同的tag
            2:可以用父视图的父视图找子视图,理解查找机制
     */
    superView.tag = 100;
    
    view2.tag = 101;
    
    <span style="color:#ff0000;">//7.开启多点触摸</span>
    superView.multipleTouchEnabled = YES;
    
   <span style="color:#ff0000;"> //8.开启响应触摸事件</span>
    superView.userInteractionEnabled = NO;
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.backgroundColor = [UIColor redColor];
    button.frame = CGRectMake(90, 90, 90, 90);
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [superView addSubview:button];
    
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 290, 40, 40)];
    view3.backgroundColor = [UIColor blueColor];
    [superView addSubview:view3];
    /*
     clipsToBounds设置为yes后,会将超过父视图的子视图部分截取
     */
    superView.clipsToBounds = YES;
    
    [view3 release];
    
    /*__________________________UIView的内存管理___________________________*/
    TestView *testView = [[TestView alloc] initWithFrame:CGRectMake(0, 400, 320, 60)];
    testView.backgroundColor = [UIColor cyanColor];
    NSLog(@"testView:%d",testView.retainCount);
    
    [self.window addSubview:testView];
    NSLog(@"testView:%d",testView.retainCount);
    
    [testView release];  //1
    NSLog(@"testView:%d",testView.retainCount);
    
    <span style="color:#ff0000;">//从父视图上移除,被释放</span>
    [testView removeFromSuperview];
    
    return YES;
}

- (void)buttonAction
{
    NSLog(@"buttonAction");
}
//程序进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    
    //通过tag值取得视图
//    UIView *view = [self.window viewWithTag:100];
//    view.backgroundColor = [UIColor redColor];
    
    UIView *view = [self.window viewWithTag:101];
    view.backgroundColor = [UIColor orangeColor];
    
}



@end


视图的层次结构

标签:bounds   center   视图的层次结构   未读信息个数   

原文地址:http://blog.csdn.net/pengyuan_d/article/details/40555025

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