将图像旋转到点
我有以下内容:
CGPoint pos //center of an image
CGPoint target //a point, somewhere in the coordinate system
float rotation //the current rotation of the image to the x-axis, clockwise, so "right" would be 90°
现在我希望图像围绕其中心点(pos)旋转,以便它直接朝向目标点。
我的想法是:计算x轴对应的角度,减去旋转,然后旋转。
有两件事:
1.)我无法计算角度。 (是的,我知道都是以拉德为单位...)
2.)什么最适合旋转?
- CGA仿射变换?但然后我需要一个 imageView
- 或者:保存上下文,将原点设置为图像中心,旋转上下文,绘制图像,恢复上下文?更复杂,但不需要图像视图......
I have the following:
CGPoint pos //center of an image
CGPoint target //a point, somewhere in the coordinate system
float rotation //the current rotation of the image to the x-axis, clockwise, so "right" would be 90°
Now I want the image to rotate around it's centerpoint (pos) so that it looks directly towards the targetpoint.
My idea was: Calculate the angle corresponding to the x-axis, subtract rotation, and then rotate it.
Two things:
1.) I fail at calculating the angle. (Yes, I know it's all in rad...)
2.) What's best for rotating?
- CGAffineTransform? But then I'd need an imageView
- Or: save context, set origin to center of image, rotate context, draw image, restore context? More complicated, but no imageview neeeded...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其绘制在 CALayer 上,然后按照您喜欢的方式移动图层。
Draw it on a CALayer, and move the layer around anyway you like.