Quick-Tip: Convert Coordinate System of a UIView

Introduction

When performing complex animations during run-time or any task that would require the translation of one UIView’s location into that of another UIView, keeping track of the math on your own can be a challenge. Luckily Apple has created a few methods to make conversions simple.

Converting the Coordinate System

Converting the Coordinate System

The conversion of one CGRect’s coordinates to another is done by one line of code that returns the converted CGRect.

CGRect convertedRect = [self.view convertRect:_anInnerSubview.frame fromView:_aSubview];

You can think of this conversion in this format:

[TheViewToTranslateTo convertRect:TheSubviewToConvert.frame fromView:SubviewsParentView]

Additional Conversion Methods

Apple’s documentation has a few of these methods if the example I’ve shown doesn’t fit your requirements.

 

Note: A much easier to read format of this post is available here.

By Torrey Betts