What is the most useful feature of Core Graphics (CGrect)?

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

+4
source share
1 answer

Useful Core Graphics Features

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 into the set screen area, which can be very convenient if you use geometric elements that are not based on separate UIViews.

bool CGRectContainsRect ( CGRect rect1, CGRect rect2 ); : The function takes two arguments. The first rectangle is always a surrounding object. The second argument either completely falls inside the first or not.

bool CGRectIntersectsRect ( CGRect rect1, CGRect rect2 ); : If you want to see if two UIViews overlap, use CGRectIntersects instead. This takes two rectangles in any order and checks if the two rectangles have any intersection point.

CGRect CGRectIntersection ( CGRect r1, CGRect r2 ); : It also takes two arguments, both CGRects, again in any order. It returns a CGRect structure, which is the actual intersection of two CGRs. There is, as you would expect, a CGRectUnion that returns the opposite function. CGRectIntersection is convenient when you not only want to check the intersection, but also use the actual rectangle that is between the 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 the rectangle that was offset (dx, dy), providing a simple translation from one point to a new point. You do not need to start calculating a new center or frame, you can just update the frame to a new offset.

CGRect CGRectInset ( CGRect rect, CGFloat dx, CGFloat dy ); : CGRectInset is probably my favorite Core Graphics rect utility. it allows you to programmatically expand or contract a rectangle. You give it an offset pair, and let the function adjust the rectangle accordingly. The function will insert the width at dx, producing a difference of two times dx, because the insert applies to both the left and the right. The height is inserted with dy for a total difference of two.

I hope you will like it.

Link: what's the most useful-core-graphics-cgrect-functions

+4
source

All Articles