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]; //設置未讀信息數 // 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 //錯誤, bounds的x和y必須為零,它可以改變設置大小,不可以改變位置 // view.bounds = CGRectMake(10, 20, 90, 90); //center是用來表示視圖的中心位置的,可以改變視圖的位置,不可以改變視圖的大小 // 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]; //交換superView兩個子視圖的位置 [superView exchangeSubviewAtIndex:0 withSubviewAtIndex:1]; //取得當前視圖的父視圖view1.superview; NSLog(@"%@",view1.superview); //如果想判斷一個視圖是否在顯示,可以判斷view.superview是否存在 //將view1從父視圖上移除 if (view1.superview != nil) { [view1 removeFromSuperview]; } //注意: // removeFromSuperview只是將視圖從父視圖上面移除,但是不是銷毀,還是在內存中存在的 NSLog(@"view1:%@",view1); /*__________________________UIView的常用屬性___________________________*/ //1.透明度,范圍是 [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.subviews得到視圖的所有子視圖 NSArray *views = superView.subviews; for (UIView *view in views) { view.backgroundColor = [UIColor darkGrayColor]; } //4.hidden 隱藏 // view2.hidden = YES; //5.superview得到當前視圖的父視圖 NSLog(@"supView:%@",superView); NSLog(@"test:%@",view2.superview); //6.tag:視圖的標簽值 //如果想通過tag得到視圖,tag設置大於等於100 /* 注意點:1:在同一個父視圖上面,兩個子視圖不能設置相同的tag 2:可以用父視圖的父視圖找子視圖,理解查找機制 */ superView.tag = 100; view2.tag = 101; //7.開啟多點觸摸 superView.multipleTouchEnabled = YES; //8.開啟響應觸摸事件 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); //從父視圖上移除,被釋放 [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