CGContext旋转 [英] CGContext rotation

查看:165
本文介绍了CGContext旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个100x100像素的图像,我想在以图像中心为中心旋转的各种角度绘制。以下代码可以工作,但围绕坐标系的原始原点(左上角)旋转,而不是翻译的位置。因此,图片不会围绕自身旋转,而是围绕屏幕的左上角旋转。

I have a 100x100 pixel image that I want to draw at various angles rotated around the center of the image. The following code works, but rotates around the original origo of the coordinate system (upper left hand corner) and not the translated location. Thus the image is not rotated around itself but around the upper left corner of the screen.

以下代码在空白应用程序的自定义视图中运行,除了这个。

The following code was run in a custom view in a blank application with nothing else but this.

- (void)drawRect:(CGRect)rect {
  CGContextRef context = UIGraphicsGetCurrentContext();
  CGContextTranslateCTM(context, -50, -50);
  CGContextRotateCTM (context, 0.3);
  CGContextTranslateCTM(context,768/2,1024/2);
  [image drawAtPoint:CGPointMake(0,0)];
}



我尝试使用CGAffineTransform做同样的,但得到相同的结果。 p>

I tried doing the same using CGAffineTransform, but got the same results.

推荐答案

旋转总是关于原点;你需要做的不是打败这个,而是要利用它。

Rotation is always about the origin; what you need to do is not to defeat that, but to take advantage of it.

这里要记住的关键是翻译移动原点。所以,如果你想让图像居中在屏幕中心,并围绕该中心旋转图像,你需要做的是:

The key thing to remember here is that translation moves the origin. So, if you want to have the image centered at the center of the screen and rotate the image around that center, what you need to do is:


  1. 将原点移动到屏幕中心。

  2. 旋转所需的数量。 (想象一下,x轴的极端会像你这样移动;左端向下倾斜,右端向上倾斜。)

  3. 移动原点左和

想象图像的运动(假设图像的运动)在iPad上通过这些步骤。或者,更好的是,拿一个Post-It笔记,并手动模拟:

Imagine the motion of the image on the iPad through each of these steps. Or, better yet, take a Post-It note and simulate it by hand:


  1. 开始在iPad的角落的笔记

  2. 移动注释,使注释的一角在iPad的中心。

  3. 旋转注释。 li>
  4. 将左(相对于自身)移动宽度的一半,并将下(相对于自身)移动一半高度的一半。

您会注意到,注释最后旋转并居中。

You'll notice that the note ends up rotated and in the center.

这篇关于CGContext旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆