Quartz 2D - 从变换属性获取旋转度数?
我有一些 UIView 和几个用户创建的子视图。用户可以移动和旋转子视图。我使用 UIView 的转换属性来完成此任务。我想保存“文档”以便稍后重新创建它。
我目前正在将转换属性与其他一些数据一起写入文件。但变换属性是 6 个数字集,相反我想以某种方式获取用户所做的旋转度数并保存它。
有没有什么方法可以进行变换并恢复创建变换的总旋转?显然,当用户在屏幕上移动对象时,会完成许多小的变换。
I have some a UIView with several user created subviews. The user can move and rotate the subviews. I'm using the transform attribute of UIView to accomplish this. I want to save the "document" in order to recreate it later.
I'm currently writing out the transform attribute out to a file along with some other data. But the transform attribute is 6 number set, instead I would like to somehow get the degrees of rotation that the user made and save that.
Is there any way to take a transform and get back out the total rotation that created the transform? Obviously as the user was moving the object around the screen, many little transforms were done.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一种比卢修斯指出的数学更简单的方法。您可以使用 Core Animation 提供的辅助键路径来提取 UIView 的支持层的当前旋转:
这将为您提供视图的当前旋转(以弧度为单位)。请注意,这会报告第一个顺时针旋转半圆的正值和第二个顺时针半圆的负值,因此您需要在计算中考虑到这一点。
There is an easier way than even the math that lucius points to. You can use a helper keypath that Core Animation provides to extract the current rotation of a UIView's backing layer:
This will give you the current rotation of the view, in radians. Note that this reports positive values for the first clockwise semicircle of rotation, and negative values for the second, so you will need to account for that in your calculations.
是的,请阅读仿射变换的变换矩阵。因此,您需要计算出变换的象限(它所在的圆的 90 度部分),并取 sin() 的倒数(即 cosecant())和 cos() 的倒数(即是矩阵的
a
和c
的 secant()。我自己还没做过,但看起来很简单。Yeah, read up on The transformation matrix for an affine transformation. So you would need to figure out the quadrant of the transform (which 90-degree section of the circle it's in), and take the reciprocal of the sin() which is the cosecant(), and the reciprocal of the cos() which is the secant(), of
a
andc
of the matrix. I haven't done it myself but it seems pretty easy.