学习网友的文章,根据自己的吸收情况,精简出的要点内容,作为日后工作中的速查手册!
欣赏网友的一句话,一个东西的存在没有绝对的好与坏,只是存在时间与空间决定了它的价值。
(一)三个结构体:CGPoint、CGSize、CGRect
1. CGPoint
/* CGPoints. */
struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
2. CGSize
/* CGSizes. */
struct CGSize {
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
3.CGRect
/* CGRectangles. */
struct CGRect {
CGPoint origin;//偏移是相对父窗口的
CGSize size;
};
typedef struct CGRect CGRect;
这三个结构体均在一个头文件里:CGGeometry.h
(二)方法
1.CGRectMake
CG_INLINE CGRect
CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height)
{
CGRect rect;
rect.origin.x = x; rect.origin.y = y;
rect.size.width = width; rect.size.height = height;
return rect;
}
这个方法是make一个rect,定好origin(起点,左上角),宽与高,画出一个位置与大小确定的rect(矩形)。这个函数被声明为内联函数,一是因为它比较小,二是因为在画界面时我们要求一定的效率。这个函数在头文件里:CGGeometry.h
(三)基本界面元素
UIScreen类
UIScreen类代表了屏幕,在屏幕上显示内容要创建一个窗口承载内容,要创建一个窗口,需要一个边框(frame),含有边框信息的底层结构就CGRect。每个能够在屏幕上显示自己的对象都有一个边框,定义了他的显示区域,不过许多高层的视图类会自动计算这一信息。其他的那些类则在视图初始化时通过一个initWithFrame的初始化方法来设置。
CGRect cgRectBounds = [[UIScreen mainScreen] bounds];//返回的是带有状态栏的Rect
NSLog(@"bound's width:%f; bound's height:%f",cgRectBounds.size.width,cgRectBounds.size.height);
NSLog(@"bound's x:%f; bound's y:%f",cgRectBounds.origin.x,cgRectBounds.origin.y);
CGRect cgRectApplicationFrame = [[UIScreen mainScreen] applicationFrame];//不包含状态栏的Rect
NSLog(@"bound's width:%f; bound's height:%f",cgRectApplicationFrame.size.width,cgRectApplicationFrame.size.height);
NSLog(@"bound's x:%f; bound's y:%f",cgRectApplicationFrame.origin.x,cgRectApplicationFrame.origin.y);
cgRectBound.size.width:320.000000; cgRectBound.size.height:480.000000 (或者其他分辨率有所差异)
cgRectBound.origin.x:0.000000; cgRectBound.origin.y:0.000000
cgRectApplicationFrame.size.width:320.000000; cgRectApplicationFrame.size.height:460.000000;
cgRectApplicationFrame.origin.x:0.000000; cgRectApplicationFrame.origin.y:20.000000; (因为状态栏的高度是20像素)
UIView视图
UIView类继承自UIResponder,负责显示的画布,把window比作画框,就是不断地在画框上移除、更换或者叠加画布,或者在画布上叠加其他画布。这个类在UIView.h里面。
UIView* myView =[[ UIView alloc]initWithFrame:CGRectMake(0.0,0.0,200.0,400.0)];//这里创建了一块画布,定义了相对于父窗口的位置, 以及大小。
我们可以把这块画布加到其他画布上。还可以在这块画布上画上其它好玩的东东。
UIWindow窗口
UIWindow继承自UIView,关于这一点可能有点逻辑障碍,画框怎么继承自画布呢?画框的形状不就是跟画布一样吗?拿一块画布然后用一些方法把它加强,是不是可以当一个画框用呢?这也是为什么一个view可以直接加到另一个view上去的原因了。
系统初始化过程(在application didFinishLauchingWithOptions里面):
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease]; self.window.backgroundColor = [UIColor whiteColor];
(四)实际应用CGRect cgRectOne = CGRectMake(0.0, 0.0, 320, 100); UIView *uiViewOne = [[UIView alloc] initWithFrame:cgRectOne]; uiViewOne.backgroundColor = [UIColor blueColor]; [self.window addSubview:uiViewOne]; [uiViewOne release]; [self.window makeKeyAndVisible]; //让window显示出来 return YES;
//在applicationdidFinishLaunchingWithOptions里面,系统已经建好一个画框,一个应用程序只能有一个画框。 self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];//系统帮你建画框 //自己测试一下 bounds与aplicationFrame的区别 CGRect bound = [[UIScreen mainScreen]bounds]; NSLog(@"boundwidth:%f boundheight:%f ",bound.size.width, bound.size.height); NSLog(@"boundx:%f boundy:%f ",bound.origin.x, bound.origin.y); CGRect appBound = [[UIScreen mainScreen]applicationFrame]; NSLog(@"appBoundwidth:%f appBoundheight:%f ",appBound.size.width,appBound.size.height); NSLog(@"appBoundx:%f appBoundy:%f ",appBound.origin.x, appBound.origin.y); //画第一块画布然涂成蓝色,大小是320 X 100 CGRect CGone = CGRectMake(0.0, 0.0, 320, 100); UIView *v_one = [[UIView alloc]initWithFrame:CGone];//初始化view v_one.backgroundColor = [UIColor blueColor];// 涂成蓝色 [self.window addSubview:v_one];//直接加到画框上 //第二块注意它的位置 CGRect CGtwo = CGRectMake(0.0, 100, 160, 100);//画个矩形、初始化位置与大小 UIView *v_two = [[UIView alloc]initWithFrame:CGtwo];//初始化view v_two.backgroundColor = [UIColor redColor];//涂成红色 [self.window addSubview:v_two];//叠加到画框 //第三块注意他的位置 CGRect CGthree = CGRectMake(160, 100, 160, 100);// UIView *v_three = [[UIView alloc]initWithFrame:CGthree];// v_three.backgroundColor = [UIColor greenColor];// [self.window addSubview:v_three];// //第四块注意它的位置 CGRect CGfour = CGRectMake(0.0, 260, 320, 200);// UIView *v_four = [[UIView alloc]initWithFrame:CGfour];// v_four.backgroundColor = [UIColor orangeColor];// [self.window addSubview:v_four];// //第五块,计算一下它的位置,看看它的效果, //你可以让试一下把这段代码移到第一快初始化的上面试试,会有意想不到的效果 CGRect CGfive = CGRectMake(100, 150, 160, 200); UIView *v_five = [[UIView alloc]initWithFrame:CGfive]; v_five.backgroundColor = [UIColor yellowColor]; [self.window addSubview:v_five]; self.window.backgroundColor = [UIColor grayColor];// [self.window makeKeyAndVisible];// //最后记得release [v_one release]; [v_two release]; [v_three release]; [v_four release]; [v_five release]; return YES; }