Three location structures for iOS development

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.