CGRect Use TipsAuthor: xuguoxing Category: Code snippet, Coregraphics, Fundation Utils, IOS, Uikit release time: 2013-01-05 11:32ė6 no comment
In the development of iOS often need processing point, size and rectangle, 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.0, 20.0);
Cgpoint Center = cgpointmake (60.0, 240.0);
CGRect bounds = CGRectMake (5.0, 10.0, 100.0, 200.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 rectangular Cgrectzero; That is ((x=0.0,y= 0.9), (width=0.0,height=0.0), equivalent to CGRectMake (0,0,0,0).
Empty rectangular cgrectnull; The intersecting area of the two disjoint rectangles returns Cgrectnull, and a rectangle and a cgrectnull set are the original rectangles. , use Cgrectisnull to determine whether a rectangle is cgrectnull. Cgrectisempty returns true for both Cgrectzero and cgrectnull;
An infinite rectangle cgrectinfinite a rectangle with an infinite region, which can be judged using Cgrectisinfinite. 2. Determine whether two points, size, 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 of two values is small enough. Using the above method avoids making these judgments manually and simplifies the code. 3. Get rectangle boundary, midpoint coordinate value, long width
Gets the boundary coordinate value, that is, the maximum and minimum value of the x,y.
CGFloat Cgrectgetminx (CGRect rect);
CGFloat Cgrectgetmaxx (CGRect rect);
CGFloat cgrectgetminy (CGRect rect);
CGFloat cgrectgetmaxy (CGRect rect);
Gets the midpoint coordinate value, that is, the midpoint of the X,y
CGFloat cgrectgetmidx (CGRect rect);
CGFloat cgrectgetmidy (CGRect rect);
Get the rectangle long, wide
CGFloat cgrectgetwidth (CGRect rect);
CGFloat cgrectgetheight (CGRect rect);
4. Symmetric indent CGRect
CGRect arectangle = CGRectMake (0,0, MB);
CGRect smallerrectangle = Cgrectinset (Arectangle, m);
Result origin and size (80, 160)
5. Asymmetric indent CGRect
CGRect rect = CGRectMake (0, 0, MB);
Uiedgeinsets contentinsets = Uiedgeinsetsmake (a);
CGRect result = Uiedgeinsetsinsetrect (rect, contentinsets);
Result origin (20,10) and size (40,160)
6. Adjust decimal pixel cgrect to integer pixel
Adjusts the origin value downward to the nearest integer, and the size adjusts to the nearest integer so that the resulting cgrect can fully contain the original cgrect.
CGRect rect = CGRectMake (5, 7.5, m);
Uiedgeinsets contentinsets = Uiedgeinsetsmake (a);
CGRect result = cgrectintegral (rect);
Result origin (5,7) and size (50,30)
7. Check whether the rectangle contains a point
CGRect enemyrect = CGRectMake (0, 0, MB);
Cgpoint hitPoint = Cgpointmake (m);
if (Cgrectcontainspoint (Enemyrect, hitPoint)) {
//yes!
}
8. Check whether the two rectangles intersect
CGRect playerrect = Ccrectmake (in);
CGRect Minerect = CGRectMake (a);
if (Cgrectintersectsrect (Playerrect, minerect)) {
//ouch!
}
9. CGRect and NSString conversion
CGRect rect = CGRectMake (0, 0, MB);
NSString *rectstring = nsstringfromcgrect (rect))
CGRect newrect = cgrectfromstring (rectstring);
10.CGRect and Nsdictionary conversion
CGRect rect = CGRectMake (0, 0, MB);
Cfdictionaryref framedictref = cgrectcreatedictionaryrepresentation (rect);
Nsdictionary*framedict = [nsdictionary dictionarywithdictionary: (nsdictionary*) framedictref]; autoreleased
cfrelease (framedictref);
NSLog (@ "%@", framedict);
CGRect Newrect;
Cgrectmakewithdictionaryrepresentation ((cfdictionaryref) framedict, &rect);
CGRect and Nsvalue conversion
Only OBJETIVE-C objects can be stored in the Nsarray,nsdictionary container, C structures cannot be stored directly, and all needs to be converted to nsvalue values when the rectangles need to be stored.
CGRect rect = CGRectMake (0, 0, MB);
Nsvalue *value = [Nsvalue valuewithcgrect:rect];
CGRect newrect = [value rectvalue];