现在的位置: 首页 > 综合 > 正文

Useful Core Graphics functions

2018年05月26日 ⁄ 综合 ⁄ 共 3623字 ⁄ 字号 评论关闭

文章出处:http://blogs.oreilly.com/iphone/2008/12/useful-core-graphics-functions.html

Last week, I introduced several handy utilities that let you convert standard Core Graphics structures to and from strings. This week, I thought I'd continue to explore utility functions, moving on to ways you can work with points and rectangles for on-screen
calculations. Like the string utilities, these are functions, not methods, and are as such called using standard C rather than Objective C.

The Core Graphics library offers many geometry functions that simplify the way you manipulate and compare base structures. With these, you can determine how points and rectangles compare to each other. You can also modify your items using natural geometric
relationships. Here is a quick sampling of functions to get you started. They provide an overview of the kinds of features provided by Core Graphics geometry.

The CGRectContainsPoint function does exactly what you'd expect it to do. Whenever you want to test a point against a rectangle, this returns a simple boolean. Just pass it a CGRect and CGPoint. You can use this function to determine if a touch event falls
within a set onscreen area, which can be very handy if you're using geometric elements that aren't based on separate UIViews.

bool CGRectContainsPoint (
   CGRect rect,
   CGPoint point
);

When you are using UIViews and want to see if one view falls completely within the frame of a second, a related function CGRectContainsRect will do the checking for you. This does not check for an intersection; the union of both rectangles must be equal to
the first rectangle for this to return true. The function takes two arguments. The first rectangle is always the surrounding item. The second argument either falls fully inside the first or it does not.

bool CGRectContainsRect (
   CGRect rect1,
   CGRect rect2
);

If you want to see whether two UIViews overlap, use CGRectIntersects instead. This takes two rectangles, in any order, and checks to see if those two rectangles have any point of intersection.

bool CGRectIntersectsRect (
   CGRect rect1,
   CGRect rect2
);

Another way of approaching the problem is to check CGRectIntersection. This also takes two arguments, both CGRects, again in any order. It returns a CGRect structure, which is the actual intersection of the two CGRects. There is, as you'd expect, a CGRectUnion
that returns the opposite function. CGRectIntersection proves handy when you not only want to test the intersection but use the actual rectangle that falls between two views.

CGRect CGRectIntersection (
   CGRect r1,
   CGRect r2
);

If the CGRect produced by an intersection is the null rectangle (i.e. no intersection was found), then there is no overlap. Use CGRectIsNull to test. This function takes one argument, a CGRect and returns a boolean value of true, if the CGRect is the null rectangle
or false, if it is not. Alternatively, you can use CGRectIsEmpty, which returns true when a rectangle is null or when a rectangle has a zero height or width.

CGRect testRect = CGRectIntersection(rect1, rect2);
if (CGRectIsNull(testRect)) ...some result...

When you want to move views around the screen, the CGRectOffset function comes in handy. It returns a rectangle that has been offset by (dx, dy), providing a simple translation from one point to a new point. You don't have to start calculating a new center
or frame, you can just update the frame to the new offset.

CGRect CGRectOffset (
   CGRect rect,
   CGFloat dx,
   CGFloat dy
);

CGRectInset is probably my favorite of the Core Graphics rect utilities. it lets you expand or contract a rectangle programmatically. You pass it an offset pair, and let the function adjust the rectangle accordingly. The function will inset the width by dx,
producing a difference of two times dx, because the inset is applied to both the left and right. The height is inset by dy for a total difference of twice dy. When you want to increase the size of the rectangle, by producing an "outset", pass negative values
instead

CGRect CGRectInset (
   CGRect rect,
   CGFloat dx,
   CGFloat dy
);

Each of these functions provides a simple way to perform common functions without having to re-develop them from scratch. Yes, none of these would take more than a dozen lines of code to re-build but why bother? The CG routines are reliable and handy.

抱歉!评论已关闭.