如何使用CGAffineTransformMakeRotation?

27 cocoa-touch core-graphics quartz-2d ios

在此输入图像描述

我想用Quartz 2D绘制文字."菜单"方向错误.我希望"菜单"仍然可读,并且与X轴有45度.以下是我的代码:

CGContextSelectFont(context, "Arial", 12, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);

CGContextSetRGBFillColor(context, 0, 0, 0, 1); // 6 
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1);

CGContextSetTextMatrix(context, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));   
CGContextSetTextMatrix(context, CGAffineTransformMakeRotation(45)); 
CGContextShowTextAtPoint(context,10, 10, "Menu", 4);
Run Code Online (Sandbox Code Playgroud)

Kri*_*dra 114

CGAffineTransformMakeRotation需要以弧度为单位的角度,而不是度数.

#define DEGREES_TO_RADIANS(x) (M_PI * (x) / 180.0)
...

CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(45));
Run Code Online (Sandbox Code Playgroud)

  • 你应该在宏中的'x`周围添加额外的括号,以便安全地使用像'30 + 15`这样的表达式.使用宏会导致`(M_PI*30 + 15/180.0)`与`((M_PI*30)+(15/180.0))`相同.一个更安全的宏将是`#define DEGREES_TO_RADIANS(x)(M_PI*(x)/ 180.0)` (7认同)

小智 5

[view setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
Run Code Online (Sandbox Code Playgroud)

  • 或者只是“M_PI_2”,即“M_PI / 2” (4认同)