获取“规模”;来自 CATransform3D
我正在创建一个 iPad 应用程序,并希望用当前的视图大小更新用户。我曾经通过计算 CGAffineTransform 的比例并将 xScale 乘以(相关视图的)宽度并将 yScale 乘以高度来完成此操作。我想继续这样做,但我不再进行 2d 变换,并且我不确定使用什么方程从 CATransform3D 中提取比例信息。
你能帮忙吗?
I am creating an iPad app and want to update the user with the current size of a view. I used to be doing this by calculating the scale of a CGAffineTransform and multiplying the xScale by the width (of the related view) and the yScale by the height. I'd like to keep doing this, but I'm no longer doing 2d transformations, and I'm not sure what equation to use to extract the scale information from CATransform3D.
Can you help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要获取图层的当前比例,您只需在图层上执行
valueForKeyPath:
:其他键可以在 Apple 核心动画编程指南
To get the current scale of the layer you just perform a
valueForKeyPath:
on the layer:Other keys can be found in Apples Core Animation Programming Guide
我不熟悉该 API,但 CATransform3D 看起来像一个用于执行 3D 转换的常规 4x4 转换矩阵。
假设它只不过表示缩放、旋转和平移的组合,则可以通过计算左上角 3x3 的行或列的大小来提取缩放因子,具体取决于 CATransform3D 是否为行或列专业。
例如,如果是行优先,则 x 方向的比例为 ( m11 * m11 ) + ( m12 * m12 ) + ( m13 * m13 ) 的平方根。 y 和 z 比例同样是第二行和第三行的大小。
来自 < 的文档code>CATransform3DMakeTranslation 看来
CATransform3D
确实是行优先的。I'm not familiar with the API but
CATransform3D
looks like a regular 4x4 transformation matrix for doing 3D transformations.Assuming that it represents nothing more than a combination of scale, rotation and translation, the scale factors can be extracted by calculating the magnitudes of either the rows or columns of the upper left 3x3 depending on whether
CATransform3D
is row or column major respectively.For example, if it is row-major, the scale in the x-direction is the square root of ( m11 * m11 ) + ( m12 * m12 ) + ( m13 * m13 ). The y and z scales would similarly be the magnitudes of the second and third rows.
From the documentation for
CATransform3DMakeTranslation
it appears thatCATransform3D
is indeed row-major.这是 swift 4+ 的更新答案
Here's an update answer for swift 4+