//
// AppDelegate.h
// UI01_UIView
//
// Created by t on 15/8/31.
// Copyright (c) 2015年 . All rights reserved.
//
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
//
// AppDelegate.m
// UI01_UIView
//
// Created by t on 15/8/31.
// Copyright (c) 2015年 . All rights reserved.
//
#import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
//重寫dealloc
- (void)dealloc{
[_window release];
[super dealloc];
}
//該方法是加載成功之後執行,方法內部,建立了一個window,以及相關的子視圖.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/*
一. UIWindow
UIWindow 是UIView的子類,作用是加載圖片,以及傳遞觸摸事件給內部的視圖.一般一個App 只有一個window.
我們可以把window認爲是畫板,其內部的view認爲是畫布,我們一般不會直接操作window,而是操作其內部的view.
*/
//創建一個window,等屏大小
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
//設置window的背景顏色
self.window.backgroundColor = [UIColor whiteColor];
//把seif.window設置爲主窗口且可見.
[self.window makeKeyAndVisible];
//window 是一個視圖容器,我們今後在window上添加視圖,纔可以顯示出來,凡是添加到window上面的視圖,視圖的屬性window不爲空.
//二. UIView
//1.創建view,並且初始化
UIView *redView = [[UIView alloc] initWithFrame:(CGRectMake(50, 10, 100, 20))];
UIView *blueView = [[UIView alloc] initWithFrame:(CGRectMake(50, 30, 100, 20)) ];
UIView *blackView = [[UIView alloc] initWithFrame:(CGRectMake(50,50 , 100, 20))];
UIView *purpleView = [[UIView alloc] initWithFrame:(CGRectMake(50, 70, 100, 20))];
UIView *yellowView = [[UIView alloc] initWithFrame:(CGRectMake(50, 90, 100, 20))];
UIView *grayView = [[UIView alloc] initWithFrame:(CGRectMake(50, 110, 100, 20))];
UIView *greenView = [[UIView alloc]initWithFrame:(CGRectMake(50, 130, 100, 20))];
// 第二種方式,init和frame賦值分開來寫.
// UIView *redView1 = [[UIView alloc]init];
// redView1.frame = CGRectMake(150, 150, 150, 150);
/*
frame
frame 本身是一個結構體類型,CGRect(CGPoint(x, y), CGSize(width, height))
CGRect 結構體的成員是兩個結構體
CGPoint(x, y)控制view的座標
CGSize(Width, height)控制view的大小
創建CGRect 類型變量,通過CGRectMake()函數
注意:frame是子視圖在父視圖座標系中的位置.frame.origin 是子視圖的左上角距離父視圖原點的距離
在ios中,一個視圖的座標原點,默認爲左上角.
*/
UIView *orangeView = [[UIView alloc] initWithFrame:(CGRectMake(50, 150, 100, 20))];
orangeView.backgroundColor = [UIColor orangeColor];
[self.window addSubview:orangeView];
[orangeView release];
CGRect rec = orangeView.frame; //封裝,方便以後用.
rec.origin.y = 200;
orangeView.frame = rec;
//// blackView.frame.origin.x = 50; //錯誤的,產生歧義!
CGRect rec1 = blackView.frame;
rec1.origin.x = 200;
blackView.frame = rec1; //賦值
//2.設置屬性
redView.backgroundColor = [UIColor redColor];//給view的backgroundColor 背景顏色設置爲紅色
blueView.backgroundColor = [UIColor blueColor];
blackView.backgroundColor = [UIColor blackColor];
purpleView.backgroundColor = [UIColor purpleColor];
yellowView.backgroundColor = [UIColor yellowColor];
grayView.backgroundColor = [UIColor grayColor];
greenView.backgroundColor = [UIColor greenColor];
//3.添加視圖到window上
[self.window addSubview:redView];
[self.window addSubview:blackView];
[self.window addSubview:blueView];
[self.window addSubview:purpleView];
[self.window addSubview:yellowView];
[self.window addSubview:grayView];
[self.window addSubview:greenView];
//4.釋放
[redView release];
[blueView release];
[blackView release];
[purpleView release];
[yellowView release];
[grayView release];
[greenView release];
/*
view 的center 屬性
center 爲視圖的中心點,是基於frame的origin 和 size 決定的
center.x = frame.origin.x + size.width / 2
center.y = frame.origin.y + size.height / 2
*/
//**********************************************************
/*
bounds
bounds 是view的一個重要的屬性,代表的是實際顯示區域.
bounds 參考的是view 自己的座標系.
bounds.origin 爲 視圖的左上角 距離 本身座標系的位置. (視圖本身座標系原點默認爲左上角)因此bounds.origin默認爲(0, 0)
*/
UIView *view1 = [[UIView alloc] initWithFrame:(CGRectMake(100, 100, 200, 200))];
view1.backgroundColor = [UIColor magentaColor];
[self.window addSubview:view1];
//給view1 添加一個子視圖
UIView *subView = [[UIView alloc] initWithFrame:(CGRectMake(100, 100, 100, 100))];
subView.backgroundColor = [UIColor blueColor];
[view1 addSubview:subView];
//取到view1.bounds
CGRect rect = view1.bounds;
//改變bounds的origin
rect.origin.x = 100;//view1視圖左上角,距離原點爲100
view1.bounds = rect;
//改變bounds的size
CGRect rec2 = view1.bounds;
rec2.size = CGSizeMake(300, 300);
view1.bounds = rec2;
NSLog(@"%f %f", view1.bounds.origin.x, view1.bounds.origin.y);
[view1 release];
// frame bounds 總結
//frame 是父視圖左上角 在父視圖座標系中的位置. 參考系爲父視圖座標系.改變frame的origin,改變的是子視圖在父視圖中的位置
//bounds 是子視圖左上角 在本地座標系中的位置.參考系爲本地座標系.改變 bounds的origin.改變的是本視圖的座標系,視圖的原本位置不會發生變化,只是相對於原點座標的數值發生了改變.
//改變bounds 不會改變自身的位置,會改變子視圖的位置.
//bounds 是視圖的實際顯示區域.如果bounds的size和frame 的size 不同,會基於,view的中心點發生變化.
//********************************************************
UIView *red1View = [[UIView alloc] initWithFrame:(CGRectMake(100, 100, 100, 100))];
redView.backgroundColor = [UIColor redColor];
[self.window addSubview:red1View];
[red1View release];
UIView *orange1View = [[UIView alloc] initWithFrame:(CGRectMake(150, 150, 100, 100))];
orange1View.backgroundColor = [UIColor orangeColor];
[self.window insertSubview:orange1View atIndex:1];
[orange1View release];
UIView *yellow1View = [[UIView alloc] initWithFrame:(CGRectMake(200, 200, 100, 100))];
yellow1View.backgroundColor = [UIColor yellowColor];
[self.window insertSubview:yellow1View atIndex:2];
[yellow1View release];
UIView *blue1View = [[UIView alloc] initWithFrame:(CGRectMake(250, 250, 100, 100))];
blue1View.backgroundColor = [UIColor blueColor];
[self.window insertSubview:blue1View aboveSubview:yellow1View];
[blue1View release];
////insertSubView:bView belowSubView:aView 在aView 視圖的下面添加視圖bView . subView數組a對象index的前面.
UIView *black1View = [[UIView alloc] initWithFrame:(CGRectMake(225, 225, 100, 100))];
black1View.backgroundColor = [UIColor blackColor];
[self.window insertSubview:black1View belowSubview:blue1View];
[black1View release];
UIView *green1View = [[UIView alloc] initWithFrame:(CGRectMake(50, 50, 100, 100))];
green1View.backgroundColor = [UIColor greenColor];
[self.window addSubview:green1View];
[green1View release];
//管理視圖層次的方法,都只是針對於該子視圖的父視圖來說的,一個子視圖,只能有一個父視圖.
for (UIView *view in self.window.subviews) {
NSLog(@"%lu %p", [self.window.subviews count], self.window.subviews);
[view removeFromSuperview];
}
//****************************************
//練習要求:新建一個工程,創建5個視圖(aView、bView、cView、dView、eView)
UIView *aView = [[UIView alloc] initWithFrame:(CGRectMake(100, 100, 100, 100))];
aView.backgroundColor = [UIColor redColor];
[self.window addSubview:aView];
[aView release];
UIView *bView = [[UIView alloc] initWithFrame:(CGRectMake(125, 125, 100, 100))];
bView.backgroundColor = [UIColor blueColor];
[self.window insertSubview:bView atIndex:1];
[bView release];
UIView *cView = [[UIView alloc] initWithFrame:(CGRectMake(150, 150, 100, 100))];
cView.backgroundColor = [UIColor blackColor];
[self.window insertSubview:cView atIndex:2];
[cView release];
UIView *dView = [[UIView alloc] initWithFrame:(CGRectMake(175, 175, 100, 100))];
dView.backgroundColor = [UIColor purpleColor];
[self.window insertSubview:dView atIndex:3];
[dView release];
UIView *eView = [[UIView alloc] initWithFrame:(CGRectMake(200, 200, 100, 100))];
eView.backgroundColor = [UIColor orangeColor];
[self.window insertSubview:eView atIndex:4];
[eView release];
[self.window bringSubviewToFront:aView];
[self.window sendSubviewToBack:cView];
[self.window exchangeSubviewAtIndex:3 withSubviewAtIndex:4];
[cView removeFromSuperview];
//***********************************
//// //view的常用屬性
UIView *tagView = [[UIView alloc] initWithFrame:(CGRectMake(100, 100, 200, 200))];
tagView.backgroundColor = [UIColor redColor];
tagView.tag = 10;
[self.window addSubview:tagView];
[tagView release];
UIView *view = [self.window viewWithTag:10];
view.backgroundColor = [UIColor yellowColor];
tagView.hidden = NO;
tagView.alpha = 0.5;
UIView *superView = [tagView superview];
superView.backgroundColor = [UIColor yellowColor];
for (UIView *view in self.window.subviews) {
if (view.tag == 10) {
UIView *tagView = [self.window viewWithTag:10];
tagView.backgroundColor = [UIColor yellowColor];
}
}
UIView *black2View = [[UIView alloc] initWithFrame:(CGRectMake(100, 100, 100, 100))];
black2View.backgroundColor = [UIColor blackColor];
[tagView addSubview:blackView];
[black2View release];
UIView *orange2View = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, 100, 100))];
orange2View.backgroundColor = [UIColor orangeColor];
[tagView addSubview:orange2View];
[orange2View release];
//給視圖添加標記
black2View.tag = 11;
orange2View.tag = 12;
//獲取本視圖的所有子視圖
NSArray *subViews = [tagView subviews];
for (UIView *view in subViews) {
if (view.tag == 11) {
// view = [tagView viewWithTag:11];
view.backgroundColor = [UIColor blueColor];
}else if (view.tag == 12){
view.backgroundColor = [UIColor cyanColor];
}
}
return YES;
}
@end
iOS編程-------UIView
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.