获取“规模”;来自 CATransform3D

发布于 2024-10-08 03:03:41 字数 183 浏览 0 评论 0原文

我正在创建一个 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 技术交流群。

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

发布评论

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

评论(3

○愚か者の日 2024-10-15 03:03:41

要获取图层的当前比例,您只需在图层上执行 valueForKeyPath:

CGFloat currentScale = [[layer valueForKeyPath: @"transform.scale"] floatValue];

其他键可以在 Apple 核心动画编程指南

To get the current scale of the layer you just perform a valueForKeyPath: on the layer:

CGFloat currentScale = [[layer valueForKeyPath: @"transform.scale"] floatValue];

Other keys can be found in Apples Core Animation Programming Guide

任谁 2024-10-15 03:03:41

我不熟悉该 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 that CATransform3D is indeed row-major.

纵性 2024-10-15 03:03:41

这是 swift 4+ 的更新答案

guard let currentScaleX = view.layer.value(forKeyPath: "transform.scale.x") as? CGFloat {
    return 
}
print ("the scale x is \(currentScaleX)")
guard let currentScaleY = view.layer.value(forKeyPath: "transform.scale.y") as? CGFloat {
    return 
}
print ("the scale y is \(currentScaleY)")

Here's an update answer for swift 4+

guard let currentScaleX = view.layer.value(forKeyPath: "transform.scale.x") as? CGFloat {
    return 
}
print ("the scale x is \(currentScaleX)")
guard let currentScaleY = view.layer.value(forKeyPath: "transform.scale.y") as? CGFloat {
    return 
}
print ("the scale y is \(currentScaleY)")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文