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点正确显示。
Thanks for all your help!
感谢你的帮助!
4 个解决方案
#1
9
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.
让它围绕图层的中心旋转。
#2
11
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);
#3
7
hourImageView.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
CGFloat angle = hour / 12.0 * M_PI * 2.0;
hourHand.transform = CGAffineTransformMakeRotation(angle);
#4
0
Swift 4.0
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
#1
9
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.
让它围绕图层的中心旋转。
#2
11
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);
#3
7
hourImageView.layer.anchorPoint = CGPointMake(0.5f, 0.9f);
CGFloat angle = hour / 12.0 * M_PI * 2.0;
hourHand.transform = CGAffineTransformMakeRotation(angle);
#4
0
Swift 4.0
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