CGRects and Centers

As you’ve seen, UIView instances use a CGRect structure, composed of an origin and a size, to define their frame. A CGRect structure contains no references to a center point. At the same time, UIViews depend on their center property to update a view’s position when you move a view to a new point. Unfortunately, Core Graphics doesn’t use centers as a primary rectangle concept. As far as centers are concerned, the built-in utilities in Core Graphics are limited to recovering a rectangle’s midpoint along the x- or y-axis.

You can bridge this gap by constructing functions that coordinate between the origin-based CGRect struct and center-based UIView objects. Such a function retrieves the center from a rectangle by building a point ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.