01
/**
* 1、创建完空模板之后,修改环境为MRC
2、将AppDelegate.h文件中的strong改为retain
3、在AppDelegate.m文件中重写dealloc 方法将实例变量_window释放一次
4、在创建window对象的最后加autorelease,完成内存管理。
*/
- (void)dealloc {
[_window release];
[_containerView release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建一个窗口对象,并且和屏幕大小一样。
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//设置窗口的背景色为白色
self.window.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1.0];
//初始化与窗口等大的视图。
//将window比作画板的话,此视图就是画板上的画纸。
//创建一个UIView对象的过程
//1、初始化,并且指定大小
_containerView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//2、设置背景颜色
//视图如果不给定背景色,默认是透明色
_containerView.backgroundColor = [UIColor whiteColor];
//3、添加显示,将视图添加到window上面进行显示。
[self.window addSubview:_containerView];
//创建一个红色视图,添加到containerView上面进行显示
//一个视图默认其左上角点就是这个视图的坐标系原点
//并且每一个视图都有自己的坐标系
//一个视图布局时,frame中的x、y是相对于父视图坐标系原点的距离。
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
NSLog(@"view.frame is %@",NSStringFromCGRect(view.frame));
view.bounds = CGRectMake(0, 0, 250, 250);
NSLog(@"view.frame is %@",NSStringFromCGRect(view.frame));
NSLog(@"view.center is %@",NSStringFromCGPoint(view.center));//此函数可以将结构体CGPoint转换为字符串,进行打印。
//center、视图中心点
CGPoint newCenter = CGPointMake(0, 0);
//一个视图的中心点与视图的frame是息息相关的。center与frame都是相对于父视图来说的。
//视图中心点center.x 为 视图本身frame中的x值加上视图本身宽的一半
//视图中心点center.y 为 视图本身frame中0的y值加上视图本身高的一半
newCenter.x = view.frame.origin.x + view.frame.size.width / 2;
newCenter.y = view.frame.origin.y + view.frame.size.height / 2;
view.center = newCenter;
NSLog(@"view.center is %@",NSStringFromCGPoint(view.center));//此函数可以将结构体CGPoint转换为字符串,进行打印。
view.backgroundColor = [UIColor redColor];
[_containerView addSubview:view];
[view release];
/**
1、frame、center是相对于父视图而言的,改变视图本身的frame、center会直接影响自身在其父视图上的显示位置
2、bounds是相对于自身而言的,改变bounds的值会影响自身坐标系原点的位置。进而影响子视图在其上的显示位置
3、一个视图bounds的默认值为(0, 0, 宽, 高),因为bounds前面的两个值x、y代表的含义是视图本身左上角点距离其自身坐标系原点的距离。因为视图本身坐标系与左上角点重合,所以是0
4、改变一个视图的bounds中的x、y值,不会造成自身位置的变化,因为父视图的bounds没有改变,自身的frame以及center没有任何变化,所以与父视图的关系没有任何变化,所以不会动。
*/
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
view1.backgroundColor = [UIColor yellowColor];
[view addSubview:view1];
[view1 release];
//让窗口成为应用程序的主窗口(唯一的窗口),并且可见。
[self.window makeKeyAndVisible];
return YES;
}