O'Reilly logo

iOS 7 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

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

Start Free Trial

No credit card required

20.13. 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 20.11 and in 20.12 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 CGContextRotateCTM procedure.

Let’s rotate the same rectangle we had in Figure 20-22 45 degrees clockwise (see Figure 20-32). 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 */
CGPathAddRect(path,
              &transform,
              rectangle);

As we saw in Recipe 20.12, we can also apply a transformation directly to a graphics ...

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

Start Free Trial

No credit card required