如何可靠地围绕一个点旋转图像? [英] How can I reliably rotate an image around a point?

查看:39
本文介绍了如何可靠地围绕一个点旋转图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读以下内容,来自 一步仿射围绕一个点旋转的变换?:

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."

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

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.

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

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?

--编辑--

这段代码的关键部分是:

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];

谢谢,

--编辑--

我现在减少了一些东西;如果我按照某些说明进行操作,我会:

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;

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

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

感谢您的帮助!

推荐答案

这一行:

CGAffineTransform transform = CGAffineTransformMakeTranslation(x, y);

按 x、y 移动图像.

moves the image by x, y.

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

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

这一行:

transform = CGAffineTransformRotate(transform, a);

围绕控制点旋转.

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

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

您需要设置:

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

让它围绕层的中心旋转.

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

这篇关于如何可靠地围绕一个点旋转图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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