17.11. Displacing Shapes Drawn on Graphic Contexts


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.


Use the CGAffineTransformMakeTranslation function to create an affine translation transformation.


Recipe 17.8 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 17.13) and scaling (see Recipe 17.12). 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 6 Programming Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.