I generally do not use Interface Builder to draw the interface, but with pure code to create the interface, not to install B, but just from VI to Xcode soon, not too accustomed to interface builder. Of course I'll use it if I need to. The existence of a thing is not absolutely good and bad, but the existence of time and space determines its value.
(forgot to say, my environment is xcode4.2)
First you need to understand a few basic concepts.
A) Three structural bodies: Cgpoint, Cgsize, CGRect
1. Cgpoint
[Plain] View plaincopy
- /* Points. */
- struct Cgpoint {
- CGFloat x;
- CGFloat y;
- };
- typedef struct CGPOINT Cgpoint;
See this must you already understand, no longer explain.
2. Cgsize
[Plain] View plaincopy
- /* Sizes. */
- struct Cgsize {
- CGFloat width;
- CGFloat height;
- };
- typedef struct CGSIZE cgsize;
Not explained.
3.CGRect
[Plain] View plaincopy
- /* rectangles. */
- struct CGRect {
- Cgpoint origin;//Offset is relative to the parent window
- Cgsize size;
- };
- typedef struct CGRECT CGRect;
Also not explained.
These three structures are in a header file: CGGeometry.h
II) Several methods
1.CGRectMake
[Plain] View plaincopy
- 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;
- }
Yes, this method is make a rect, set the origin (beginning, upper left corner), width and height, you can draw a position and size of a rect (rectangle) This function is declared as an inline function, one because it is smaller, and the second is because we require a certain degree of efficiency in the drawing interface. This function is still hidden in the first file: CGGeometry.h
c) Several basic interface elements: Window (Windows), Views (view)
To display content on the screen first to create a window hosting content, to create a window, you need a border (frame), the underlying structure containing the border information is cgrect. Each object that can display itself on the screen has a border that defines his display area, but many high-level view classes automatically calculate this information. Other classes are set by a initWithFrame initialization method when the view is initialized.
Come to know a class: UIScreen. The UIScreen class represents the screen, and through this class we can get some of the stuff we want.
[Plain] View plaincopy
- CGRect screenbounds = [[UIScreen mainscreen]bounds];//returns a rect with the status bar
- CGRect viewbounds = [[UIScreen mainscreen]applicationframe];//does not contain a rect of the status bar
- Screenbounds and Viewbounds are relative to the device screen.
- So screenbounds.origin.x== 0.0; SCREENBOUNDS.ORINGIN.Y = 0.0; ScreenBounds.size.width = = 320; ScreenBounds.size.height = = 480 (or some other resolution is different)
- So screenbounds.origin.x== 0.0; SCREENBOUNDS.ORINGIN.Y = 20.0; (because the height of the status bar is 20 pixels) screenBounds.size.width = = 320; ScreenBounds.size.height = = 480
UIView
Here's a look at the UIView class, which inherits from Uiresponder, and we know that it's the canvas that is responsible for the display, if you're comparing window to a frame. We are constantly removing, replacing, or overlaying the canvas on the frame, or overlaying other canvases on the canvas, which is of course the painter's decision. With the canvas, we can do it arbitrarily on top. A lot of simple things I will put the contents of the library, if things too much paste out is not very good, friends go to the library file inside to see it. This class is inside the UIView.h. Below we first learn some basic things, the other stuff will slowly unfold in the future.
[Plain] View plaincopy
- uiview* MyView =[[UIView alloc]initwithframe:cgrectmake (0.0,0.0,200.0,400.0)];//Here you create a canvas that defines the position relative to the parent window, and the size.
We can add this canvas to the other canvas, which we'll talk about later in the process. We can also draw on this canvas other interesting things, the specific situation will be explained in the following.
UIWindow
UIWindow inherited from UIView, there may be a logical obstacle to this point, how does the frame inherit from the canvas? Do not go too far to niu Jiao Jian, the shape of the frame is the same as the canvas? Take a canvas and then use some methods to strengthen it, is it possible to use it as a frame? That's why a view can be added directly to another view.
Take a look at the initialization process of the system (inside Application Didfinishlauchingwithoptions):
[Plain] View plaincopy
- Self.window = [[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease];
- Self.window.backgroundColor = [Uicolor graycolor];//sets a background color for window
Several basic concepts of the iphone drawing Cgpoint, Cgsize, CGRect, CGRectMake, window, Views (view)