ios 如何可靠地围绕一个点旋转图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19304470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:16:45  来源:igfitidea点击:

How can I reliably rotate an image around a point?

iosobjective-ctransformationaffinetransform

提问by Christos Hayward

I've read the following, from One step affine transform for rotation around a point?:

我已经阅读了以下内容,从绕点旋转的一步仿射变换?

CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);
transform = CGAffineTransformRotate(transform, a);
transform = CGAffineTransformTranslate(transform,-x,-y);

However, when I do this the images transformed are all over the map, and rarely over the screen.

但是,当我这样做时,转换后的图像遍布整个地图,很少出现在屏幕上。

I've tried a bit to make a system where it will place images; if you run a loop and place several values, a spiral appears. However, while I might be able to eventually untangle the relationship between a rotation of images and the points I wanted them to rotate about, I wanted to ask for the best solution to "I have an image here; I consider this point to be its center; I want it to be rotated by this amount around its center but not otherwise displaced."

我已经尝试制作一个可以放置图像的系统;如果您运行一个循环并放置多个值,则会出现一个螺旋。然而,虽然我可能最终能够解开图像旋转与我希望它们旋转的点之间的关系,但我想寻求最佳解决方案“我在这里有一个图像;我认为这一点是它的中心;我希望它围绕其中心旋转这个量,但不以其他方式移位。”

I am trying to make a modified port of http://JonathansCorner.com/ancient-clock/, and right now I am trying to place the hour hand. All attempts to do the song and dance above, and translate it so its center is at the desired center, have failed.

我正在尝试修改http://JonathansCorner.com/ancient-clock/ 的端口,现在我正在尝试放置时针。所有尝试做上面的歌曲和舞蹈,并将其翻译成其中心在所需的中心,都失败了。

How, for the hands of this clock, can I say "I want the hands in the following rotations" and have them appropriately placed around the center?

对于这个时钟的指针,我如何能说“我想要指针在以下旋转”并将它们适当地放置在中心周围?

--EDIT--

- 编辑 -

The crucial part of this code, edited in an attempt to use layers in response to a comment, is:

这段代码的关键部分是为了使用层来回应评论而编辑的:

UIImage *hourHandImage = [UIImage imageNamed:@"hour-hand.png"];
UIImageView *hourHandView = [[UIImageView alloc] initWithImage:hourHandImage];
float hourRotation = .5;
hourHandImage = [UIImage imageNamed:@"hour-hand.png"];
hourHandView = [[UIImageView alloc] initWithImage:hourHandImage];
CGAffineTransform transform = CGAffineTransformMakeTranslation(centerX - 21, -centerY - 121);
// transform = CGAffineTransformRotate(transform, hourRotation);
// transform = CGAffineTransformTranslate(transform, 2 * centerX, 2 * centerY);
hourHandView.transform = transform;
hourHandView.layer.anchorPoint = CGPointMake(centerX, centerY);
hourHandView.layer.affineTransform = CGAffineTransformRotate(transform, hourRotation);
[self.view addSubview:hourHandView];

Thanks,

谢谢,

--EDIT--

- 编辑 -

I now have something pared down; if I follow some of the instructions, I have:

我现在减少了一些东西;如果我遵循一些说明,我有:

CGAffineTransform transform = CGAffineTransformMakeTranslation(100 -21, 100 -121);
transform = CGAffineTransformRotate(transform, hour / 12.0 * M_PI * 2.0);
transform = CGAffineTransformTranslate(transform, -330, 330);
hourHandView.transform = transform;

I'd like to work on getting those numbers out, but this has an hour of 9:00 correctly displayed.

我想努力把这些数字弄出来,但这有一个小时的 9:00 正确显示。

Thanks for all your help!

感谢你的帮助!

采纳答案by HalR

This line:

这一行:

CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);

moves the image by x, y.

将图像移动 x, y。

(Note-- you will spin around the control point, whatever you've set that to be)

(注意——你将围绕控制点旋转,无论你设置的是什么)

this line:

这一行:

transform = CGAffineTransformRotate(transform, a);

rotates it around the control point.

围绕控制点旋转。

If your control point is in the top left hand corner (the default), it will spin around the top lefthand corner.

如果您的控制点位于左上角(默认),它将围绕左上角旋转。

You need to set this:

你需要这样设置:

[self layer].anchorPoint = CGPointMake(0.5, 0.5);

to get it to spin around the center of the layer.

让它围绕层的中心旋转。

回答by AlexWien

A rotation is always done around (0,0).

旋转始终围绕 (0,0) 进行。

To translate around a point you FIRST need to translate the rotate point to (0,0) and then rotate it, and then translate it back.

要围绕一个点平移,您首先需要将旋转点平移到 (0,0),然后旋转它,然后将其平移回来。

So it should be:

所以应该是:

// cx, cy is center of screen
// move (cx,cy) to (0,0)  
CGAffineTransform transform = CGAffineTransformMakeTranslation(-cx, -cy);

// roate around (0,0)  
transform = CGAffineTransformRotate(transform, angleRadians);

// mov e (0,0) back to (cx,cy)  
transform = CGAffineTransformTranslate(transform,cx,cy);

回答by Jano

hourImageView.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
CGFloat angle = hour / 12.0 * M_PI * 2.0;
hourHand.transform = CGAffineTransformMakeRotation(angle);

回答by Aaron Halvorsen

Swift 5 version from Alex's answer

来自亚历克斯回答的 Swift 5 版本

A rotation is always done around (0,0).

旋转始终围绕 (0,0) 进行。

To translate around a point you FIRST need to translate the rotate point to (0,0) and then rotate it, and then translate it back.

要围绕一个点平移,您首先需要将旋转点平移到 (0,0),然后旋转它,然后将其平移回来。

So it should be:

所以应该是:

// centerX, centerY is center of screen
// move (centerX,centerY) to (0,0)  
var myTransform = CGAffineTransform(translationX: -centerX, y: -centerY)

// rotate around (0,0)  
myTransform = myTransform.concatenating(CGAffineTransform(rotationAngle: angleInRadians))

// move (0,0) back to (cx,cy)  
myTransform = myTransform.concatenating(CGAffineTransform(translationX: centerX, y: centerY))

myView.transform = myTransform