## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# 15.12. Rotating Shapes Drawn on Graphic Contexts

## Problem

You want to be able to rotate the contents that you have drawn on a graphics context without changing your drawing code.

## Solution

Use the `CGAffineTransformMakeRotation` function to create an affine rotation transformation.

## Discussion

### Note

I strongly suggest that you read the material in Recipes 15.10 and in 15.11 before proceeding with this section. To avoid redundancy, I have tried to keep material that has been taught in earlier sections out of later sections.

Just like scaling and translation, you can apply rotation translation to shapes drawn on paths, and graphics contexts. You can use the `CGAffineTransformMakeRotation` function and pass the rotation value in radians to get back a rotation transformation, of type `CGAffineTransform`. You can then apply this transformation to paths and shapes. If you want to rotate the whole context by a specific angle, you must use the `CGContext``RotateCTM` procedure.

Let’s rotate the same rectangle we had in Figure 15-21 45 degrees clockwise (see Figure 15-31). The values you supply for rotation must be in radians. Positive values cause clockwise rotation, while negative values cause counterclockwise rotation:

```/* Rotate the rectangle 45 degrees clockwise */
CGAffineTransform transform =
CGAffineTransformMakeRotation((45.0f * M_PI) / 180.0f);

/* Add the rectangle to the path */