Search code examples
iphoneiosquartz-graphicscgaffinetransform

Need examples for how to use CGAffineTransform in CGPaths


I don't understand how to apply "CGAffineTransform" parameter that appears in practically every CGPath method, e.g.:

void CGPathAddRect (
   CGMutablePathRef path,
   const CGAffineTransform *m,
   CGRect rect
);

Let's say I want to rotate a rect path, how would I write out this function? Where to I get the transform matrix?


Solution

  • You use use CGAffineTransformMakeRotation to create a CGAffineTransform that rotates the rectangle about the point (0, 0).

     CGAffineTransform rotation = CGAffineTransformMakeRotation(M_PI / 4);  // π/4 = 45°
     CGPathAddRect(path, &rotation, CGRectMake(0, 0, 80, 40));
    

    If you need it to rotate about any other points (x, y), you need to compose 2 translations to move (x, y) to (0, 0) and back.