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 CGContext
RotateCTM
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 ...
Get iOS 7 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.