CGRect Tips for use

Source: Internet
Author: User

In iOS development, we often need to deal with points, sizes and rectangles, the corresponding data structure is cgpoint,cgsize,cgrect, these data structures are C structure, the definition is as follows:

Cgpoint {  cgfloat x;  CGFloat y;}; cgsize {  cgfloat width;  CGFloat height;}; struct CGRect {  cgpoint origin;  Cgsize size;};

There are some handy ways to manipulate these data structures, as summarized below:

1. Create Cgpoint,cgsize,cgrect
Cgsize size = Cgsizemake (10.020.0= Cgpointmake (60.0240.0  = CGRectMake (5.010.0100.0200.0);

Some special cases
0 o ' Cgpointzero; i.e. (x=0.0, y=0.0), equivalent to Gpointmake (0,0);
0 size Cgsizezero; i.e. (width=0.0, height= 0.0), equivalent to Cgsizemake (0,0);
0 Rectangle Cgrectzero; i.e. ((x=0.0,y= 0.0), (width=0.0,height=0.0)), equivalent to CGRectMake (0,0,0,0).
Empty rectangular cgrectnull; The intersection of two disjoint rectangles returns Cgrectnull, and the cgrectnull of a rectangle is the original rectangle. , use Cgrectisnull to determine if a rectangle is cgrectnull. Cgrectisempty returns true for both Cgrectzero and cgrectnull;
The infinite rectangle cgrectinfinite has an infinite area of the rectangle, which can be judged using Cgrectisinfinite.

2. Determine two points, size, whether the rectangle is equal
BOOL Cgpointequaltopoint (Cgpoint point1, Cgpoint point2); BOOL cgsizeequaltosize (cgsize size1, cgsize size2); BOOL Cgrectequaltorect (CGRect rect1, CGRect rect2);

Cgpoint,cgsize,cgrect internal data are cgpoint, for floating-point numbers, because there may be rounding errors, can not directly determine whether the two are equal, but should determine whether the difference between the two values is small enough. Using the above method avoids making these judgments by hand and simplifying the code.

3. Get rectangle boundary, midpoint coordinate value, length width

1) Gets the boundary coordinate value, which is the maximum and minimum value of x, y .

CGFloat Cgrectgetminx (CGRect rect); CGFloat Cgrectgetmaxx (CGRect rect); CGFloat cgrectgetminy (CGRect rect); CGFloat cgrectgetmaxy (CGRect rect);

2) Gets the midpoint coordinate value, which is the midpoint of X, y

CGFloat cgrectgetmidx (CGRect rect); CGFloat cgrectgetmidy (CGRect rect);

3) Get rectangle length, Width

CGFloat cgrectgetwidth (CGRect rect); CGFloat cgrectgetheight (CGRect rect);
4. Symmetrical indent CGRect
CGRect arectangle = CGRectMake (0,0); // result origin (ten) and size (+)
5. Asymmetric indent CGRect
CGRect rect = CGRectMake (00= Uiedgeinsetsmake (20  uiedgeinsetsinsetrect= (rect, contentinsets); // result Origin (20,10) and size (40,160)
6. Adjust fractional pixels cgrect to integer pixels

The origin value is adjusted downward to the nearest integer, and size is adjusted upward to the nearest integer so that the resulting cgrect can fully contain the original cgrect.

CGRect rect = CGRectMake (57.5= Uiedgeinsetsmake (20  += cgrectintegral (rect); // result Origin (5,7) and size (50,30)
7. Check if the rectangle contains a point
CGRect enemyrect = CGRectMake (00= Cgpointmake (50 ); if (Cgrectcontainspoint (Enemyrect, hitPoint))    {        //  yes!}
8. Check whether the two rectangles intersect
CGRect playerrect = Ccrectmake (a);     = CGRectMake (ten);     if (Cgrectintersectsrect (Playerrect, minerect))    {        //  ouch!    }
9. CGRect and NSString Conversion
CGRect rect = CGRectMake (00);    *rectstring = nsstringfromcgrect (rect))   = cgrectfromstring (rectstring);
10.CGRect and Nsdictionary Conversion
CGRect rect = CGRectMake (00=  Cgrectcreatedictionaryrepresentation (rect); Nsdictionary//  autoreleasedcfrelease (framedictref); NSLog (@ "%@"&rect);
CGRect and Nsvalue Conversion

Only OBJETIVE-C objects can be stored in the Nsarray,nsdictionary container, and C structures cannot be stored directly, and all need to be converted to Nsvalue values when the rectangle needs to be stored.

CGRect rect = CGRectMake (00*value == [value Cgrectvalue] ;

CGRect Tips for use

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.