• 视图的层次结构


    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
    


  • 相关阅读:
    innerHTML和outerHTML的区别
    递归,汉诺塔
    js 中的 Math.ceil() Math.floor Math.round()
    JS中的异常exception
    CSS样式中visited,hover,active , focus这四个分别表示什么意思?
    用户在设置密码时,提醒请输入半角字符(vue+element+valid)
    设置用户密码时,将全角转换为半角
    后台返回对象数组,对象属性相同时,只取一个对象
    远程链接mongoDB robomongo
    mongodb 入坑
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/7290605.html
Copyright © 2020-2023  润新知