For iOS development, we need to specify the position where controls, text, images, and so on are displayed in the view, so we will inevitably deal with the following three structures.
Cgpoint
Cgsize
Cgrect
Note: These three are structures, not classes, so pointers are generally not used. The following describes the definitions of these three structures.
Struct cgpoint {
Cgfloat X;
Cgfloat y;
};
Typedefstructcgpoint cgpoint;
/* Sizes .*/
Struct cgsize {
Cgfloat width;
Cgfloat height;
};
Typedefstructcgsize cgsize;
/* Rectangles .*/
Struct cgrect {
Cgpoint origin;
Cgsize size;
};
Typedefstructcgrect cgrect;
Shows the relationship between cgrect and the former two:
The three structures have a corresponding generation function:
Generate Functions |
Example |
Cgpointmake (x, y) |
Cgpoint point = cgpointmake (100.0, 200.0 );
Point. x = 300.0; Point. Y = 30.0; |
Cgsizemake (width, height) |
Cgsize size = cgsizemake (42.0, 11.0 ); Size. width = 100.0; Size. Height = 72.0; |
Cgrectmake (X, Y, width, height) |
Cgrect rect = cgrectmake (100.0, 200.0, 42.0, 11.0 ); Rect. Origin. x = 0.0; Rect. Size. size = 50.0; |
In IOS programming, the coordinate system is described as follows:
Regardless of the device direction, the origin point must be in the upper left corner of the screen. The Y axis increases from top to bottom, and the X axis increases from left to right.
Notes
1. Coordinates and lengths are float,
2. you can specify a value outside the screen, or even a negative value. In this case, you can think of the screen in a flute coordinate system. However, unlike a normal flute coordinate system, the first quadrant is in the lower right corner.