15.10. Displacing Shapes Drawn on Graphic Contexts

Problem

You want to move everything that is drawn on a graphics context to a new location, without changing your drawing code—or you would simply like to displace your context’s contents with ease.

Solution

Use the CGAffineTransformMakeTranslation function to create an affine translation transformation.

Discussion

Recipe 15.7 mentioned transformations. These are exactly what the name suggests: changes to the way a graphic is displayed. Transformations in Core Graphics are objects that you apply to shapes before they get drawn. For instance, you can create a translation transformation. Translating what, you might be asking? A translation transformation is a mechanism by which you can displace a shape or a graphics context.

Other types of transformations include rotation (see Recipe 15.12) and scaling (see Recipe 15.11). These are all examples of affine transformations, which map each point in the origin to another point in the final version. All the transformations we discuss in this book will be affine transformations.

A translation transformation translates the current position of a shape on a path or graphics context to another relative place. For instance, if you draw a point at location (10, 20), apply a translation transformation of (30, 40) to it, and then draw it, the point will be drawn at (40, 60), because 40 is the sum of 10+30 and 60 is the sum of 20+40.

In order to create a new translation transformation, we must use the CGAffine ...

Get iOS 5 Programming Cookbook 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.