Abhishek Bedi
Abhishek Bedi

Reputation: 5451

What the most useful Core Graphics (CGrect) functions?

I usually use CGRectMake method for all my code. Are there other useful handy methods ?

Upvotes: 4

Views: 1525

Answers (1)

Abhishek Bedi
Abhishek Bedi

Reputation: 5451

Useful Core Graphics functions

NSLog(@"%@", CGRectCreateDictionaryRepresentation(rect)); : Printing CGRect in NSLog

bool CGRectContainsPoint ( CGRect rect, CGPoint point ); : 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 CGRectContainsRect ( CGRect rect1, CGRect rect2 ); : 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 CGRectIntersectsRect ( 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.

CGRect CGRectIntersection ( CGRect r1, CGRect r2 );: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 testRect = CGRectIntersection(rect1, rect2);if (CGRectIsNull(testRect)) ...some result...

CGRect CGRectOffset ( CGRect rect, CGFloat dx, CGFloat dy );: 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 CGRectInset ( 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.

Hope you Like it.

Reference: what-the-most-useful-core-graphics-cgrect-functions

Upvotes: 4

Related Questions