UIView详解

来源:互联网 发布:淘宝口碑好的大码女装 编辑:程序博客网 时间:2024/06/05 10:30
#import "AppDelegate.h"// 宏#define WIDTH self.window.frame.size.width#define HEIGHT self.window.frame.size.height// 通过改变视图中心点center的位置可以改变视图的位置// center.x = frame.origin.x + frame.size.width/2;// center.y = frame.origin.y + frame.size.height/2;@interface AppDelegate ()@property(nonatomic, retain)UIView *myView;@end@implementation AppDelegate- (void)dealloc{    [_myView release];    [_window release];    [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Frame 尺寸,框架    // 创建一个和屏幕一般大的window    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    // 给window一个背景颜色    // cyanColor 天蓝色 magentaColor 洋红色    self.window.backgroundColor = [UIColor magentaColor];    // 让当前的window在应用程序中可见并显示出来    [self.window makeKeyAndVisible];    // 对window进行释放    [_window release];    // UIView,创建一个UIView分四步    // 1.创建UIView对象    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 150, 150)];    // 2.给view1设置背景颜色    view1.backgroundColor = [UIColor blueColor];    // 3.把视图贴到窗口上    [self.window addSubview:view1];    // 4.释放    [view1 release];    // tag:给视图添加标记,被加完标记的视图可以使用viewWithTag:方法取出    // tag值不能是0,不能重复 这里最好设置稍微大一些    view1.tag = 1000;    UIView *tempView = [self.window viewWithTag:1000];    tempView.backgroundColor = [UIColor whiteColor];    [self.window addSubview:tempView];    NSLog(@"%@", tempView);    [tempView release];    // 两个视图地址相同    NSLog(@"%p", tempView);    NSLog(@"%p", view1);    // 设置view时,分先后顺序,如果他们所占位置重合,后设定会盖住之前设定的view窗口    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(150, 150, 150, 150)];    view2.backgroundColor = [UIColor yellowColor];    [self.window addSubview:view2];    [view2 release];    // 透明度(注意这条属性同样作用于该视图的子视图上,取值范围0 ~ 1)    view2.alpha = 1;    // 控制视图的显隐    // view2.hidden = YES;    // 一个视图可以有多个子视图,但是一个视图只能有一个父视图    [view1 addSubview:view2];    // 获取本视图的父视图    // 视图坐标的起始位置是在自己父视图的左上角    UIView *superView = [view1 superview];    NSLog(@"%@", superView);    // 获取本视图的所有子视图    NSArray *subviews = [view1 subviews];    NSLog(@"%@", subviews);    // 先创建,先添加到subview的视图会在层级关系的最下面    // (0,0)位置为屏幕的左上角 目前这个屏幕宽长375 长为667//    UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(180, 180, 100, 100)];//    view3.backgroundColor = [UIColor grayColor];//    [self.window addSubview:view3];//    [view3 release];    // 用视图的frame属性,对视图位置大小进行重新设置//    view3.frame = CGRectMake(200, 200, 100, 100);    //  bounds(边界)也是view的重要属性,⽤于定义自己的边界。它同frame一样是一个CGRect结构体变量    //  当⼀个view设置bounds时,会把自⼰当成一个容器,定义自⼰的边界⼤小以及左上角的初始坐标。当子视图添加到此视图时,会根据bounds指定的原点(0,0)计算 frame,⽽⾮左上角    self.myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 150, 150)];    self.myView.backgroundColor = [UIColor redColor];    [self.window addSubview:self.myView];    [self.myView release];//  self.myView.bounds.origin = CGPointMake(10, 0); // 这条不对, 不知道怎么用bounds属性    // 视图添加到父视图的数组之后,数组会增加视图的引用计数,相应的也就可在添加之后对视图进行释放    // 一直到frame都是getter方法 对属性进行取值 frame后的点语法实际上是对结构体成员变量进行调用    NSLog(@"%g", self.window.frame.size.width);    NSLog(@"%g", self.window.frame.size.height);    // 在指定的index处插入子视图    // 这个相当于从上往下查出父视图中添加子视图的个数,按数组下标顺序相应添加出该视图在其他视图的上面还是下面    [self.window insertSubview:self.myView atIndex:1];  // 这个下标相当于插入    // 其优先级性要比sendSubviewToBack:和bringSubviewToFront:还要高    // 在指定的视图上面添加子视图    UIView *view11 = [[UIView alloc] initWithFrame:CGRectMake(75, 75, 150, 150)];    view11.backgroundColor = [UIColor purpleColor];    [self.window addSubview:view11];    [view11 release];    [self.window insertSubview:view11 aboveSubview:view1];    // 在指定的视图下面添加子视图    UIView *view22 = [[UIView alloc] initWithFrame:CGRectMake(125, 125, 150, 150)];    view22.backgroundColor = [UIColor orangeColor];    [self.window addSubview:view22];    [view22 release];    [self.window insertSubview:view22 belowSubview:view2];//    // 通过父视图来管理他身上所有子视图的层级关系//    // 父视图把指定的子视图放在最上面//    [self.window bringSubviewToFront:view1];//    // 指定子视图移动到最后面//    [self.window sendSubviewToBack:view2];//    // 交换两个指定索引位置的子视图,改变它们之间的层级关系而已,颜色不会交换//    [self.window exchangeSubviewAtIndex:1 withSubviewAtIndex:3];    // 想知道怎么回事看这里//    //    // 把子视图从父视图上移除//    [self.myView removeFromSuperview];    // 视图的边界原点(即视图的左上角起点)//    UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(90, 200, 200, 100)];//    redView.backgroundColor = [UIColor redColor];//    [self.view addSubview:redView];//    [redView release];//    UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 50, 50)];//    blueView.backgroundColor = [UIColor blueColor];//    [redView addSubview:blueView];//    [blueView release];    /*     * 视图的bounds属性,属性的属性origin不是这么用的     * redView.bounds.origin = CGPointMake(10, 0);     */    return YES;}@end
0 0
原创粉丝点击