UI编程-UIView及其⼦子类

//
//  AppDelegate.m
//  UI01_UIView_UILabel
//
//  Created by dllo on 15/7/10.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
#pragma mark - 知识点1 UIWindow
    
    /* 创建一个和屏幕一样大小的UIWindow对象 */
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    // Override point for customization after application launch.
    
    /* 将window属性的背景颜色设置为白色, 注意是UIColor对象 */
    self.window.backgroundColor = [UIColor whiteColor];
    
    /* 使window成为主window并可见 */
    [self.window makeKeyAndVisible];
    
#pragma mark - 知识点2 UIView
    
    /* 创建UIView对象 */
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 200, 200)];
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(120, 120, 200, 200)];
    
    /* 获取屏幕的宽和高 */
    CGFloat f1 = [UIScreen mainScreen].bounds.size.width;
    CGFloat f2 = [UIScreen mainScreen].bounds.size.height;
    NSLog(@"width:%f", f1);
    NSLog(@"height:%f", f2);
    
    /* 设置view背景颜色 */
    view1.backgroundColor = [UIColor greenColor];
    view2.backgroundColor = [UIColor redColor];
    
    /* 将view1对象添加到window上 */
    [self.window addSubview:view1];/* view1的引用计数会 +1 */
    
    /* frame 属性, 它的参照坐标系是父视图
     * 将view2 添加在view1 上
     */
//    [view1 addSubview:view2];
    
    /* center 属性, 它的参照坐标系是父视图 */
//    view2.center = CGPointMake(20, 20);
    
    /* view的其他API */
    /* 透明度, 注意子视图也会更改 */
    view1.alpha = 0.7;
    
    /* view 的子视图 */
    NSArray *arr = view1.subviews;
    NSLog(@"%@", arr);
    NSLog(@"%@", self.window.subviews);
    
    /* 获取view 的父视图 */
    NSLog(@"%@", view2.superview);
    NSLog(@"%@", self.window.superview);
    
    /* 视图层级管理 */
    /* view1, view2 都是self.window子视图 */
    [self.window addSubview:view2];
    
    /* 把view1放在最前面 */
    [self.window bringSubviewToFront:view1];
    
    /* 创建一个新的视图view3, 放在view1 和 view2 之间 */
    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(80, 80, 200, 200)];
    view3.backgroundColor = [UIColor orangeColor];
    [self.window insertSubview:view3 atIndex:1];
    
    /* 创建一个新的视图view4, 放在view1 和 view3之间 */
    UIView *view4 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    view4.backgroundColor = [UIColor grayColor];
    [self.window insertSubview:view4 aboveSubview:view3];
    
    /* 更改view1 和 view2 的位置 */
    [self.window exchangeSubviewAtIndex:0 withSubviewAtIndex:3];
    
    /* hidden 属性 */
    view4.hidden = YES;
    NSLog(@"%@", self.window.subviews);
    
    /* tag 属性 */
    view1.tag = 100;
    view2.tag = 200;
    view3.tag = 300;
    view4.tag = 400;
    NSLog(@"%@", self.window.subviews);
    
    /* 根据tag 获取view对象 */
    UIView *findView = [self.window viewWithTag:100];

    /* 注意:内存问题 */
    [view1 release];
    [view2 release];
    [view3 release];
    [view4 release];
    [_window release];
    

    return YES;
}

/* MRC 下, 属性的引用计数问题, 需要重写dealloc方法 */
- (void)dealloc
{
    [_window release];
    [super dealloc];
    
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值