Quartz 2D - 从变换属性获取旋转度数?

发布于 2024-08-30 23:55:31 字数 217 浏览 5 评论 0原文

我有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷鸟归林 2024-09-06 23:55:31

有一种比卢修斯指出的数学更简单的方法。您可以使用 Core Animation 提供的辅助键路径来提取 UIView 的支持层的当前旋转:

CGFloat rotationAngle =  [[view.layer valueForKeyPath:@"transform.rotation.z"] floatValue];

这将为您提供视图的当前旋转(以弧度为单位)。请注意,这会报告第一个顺时针旋转半圆的正值和第二个顺时针半圆的负值,因此您需要在计算中考虑到这一点。

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:

CGFloat rotationAngle =  [[view.layer valueForKeyPath:@"transform.rotation.z"] floatValue];

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.

坏尐絯 2024-09-06 23:55:31

是的,请阅读仿射变换的变换矩阵。因此,您需要计算出变换的象限(它所在的圆的 90 度部分),并取 sin() 的倒数(即 cosecant())和 cos() 的倒数(即是矩阵的 ac 的 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 and c of the matrix. I haven't done it myself but it seems pretty easy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文