如何使用CGAffineTransformMakeRotation?

27

enter image description here

我想使用Quartz 2D来绘制文字。 "menu" 的方向是错误的。我希望 "Menu" 仍然可读,并且与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);
2个回答

114

CGAffineTransformMakeRotation函数需要使用弧度制表示的角度值作为参数,而非角度制。

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

CGAffineTransformMakeRotation(DEGREES_TO_RADIANS(45));

7
在宏定义中,应该在变量 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) - David Rönnqvist

5
[view setTransform:CGAffineTransformMakeRotation(M_PI / 2)];

4
可以使用 M_PI_2,它等同于 M_PI / 2 - mb21

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接