【问题标题】:iPhoneSDK calculate Rotation angle from CATransform3DiPhoneSDK 从 CATransform3D 计算旋转角度
【发布时间】:2010-08-25 11:36:09
【问题描述】:

如何通过将 CATransform3D 结构作为输入来计算绕 Z 轴的弧度旋转?

基本上我需要的是相反的CATransform3DMakeRotation

【问题讨论】:

    标签: iphone core-animation catransform3d


    【解决方案1】:

    这取决于您在哪个轴上进行旋转。

    绕z轴的旋转表示为:

    a  = angle in radians
    x' = x*cos.a - y*sin.a
    y' = x*sin.a + y*cos.a
    z' = z
    
    ( cos.a  sin.a  0  0)
    (-sin.a  cos.a  0  0)
    ( 0        0    1  0)
    ( 0        0    0  1)
    

    所以角度应该是a = atan2(transform.m12, transform.m11);

    绕x轴旋转:

    a  = angle in radians
    y' = y*cos.a - z*sin.a
    z' = y*sin.a + z*cos.a
    x' = x
    
    (1    0      0    0)
    (0  cos.a  sin.a  0)
    (0 -sin.a  cos.a  0)
    (0    0     0     1)
    

    绕y轴旋转:

    a  = angle in radians
    z' = z*cos.a - x*sin.a
    x' = z*sin.a + x*cos.a
    y' = y
    
    (cos.a  0  -sin.a   0)
    (0      1    0      0)
    (sin.a  0  cos.a    0)
    (0      0    0      1) 
    

    【讨论】:

    • 请务必使用 atan2 而不是 atan,因为后者不会在正确的象限中给出角度。
    • 谢谢 Nader,在你回答之前,我已经求助于使用 CALayer 的 affineTransform 属性将 Transform3D 转换为 AffineTransform,然后从这个计算中得到角度:stackoverflow.com/questions/2051811/… 现在我知道了,如何直接从 Transform3D 计算。谢谢!
    • 是的,我在大学一年级就知道了这一点,并且记得问我的朋友“他为什么要这样做,我们需要知道这个”
    • 您也可以使用 atan2(transform.b, transform.a) 从仿射变换计算角度,对于 z 轴旋转,数学仍然适用
    【解决方案2】:

    如果将变换附加到图层,则可以如下获取旋转的值:

    CGFloat angle = [(NSNumber *)[layer valueForKeyPath:@"transform.rotation.z"] floatValue];
    

    来自documentation

    Core Animation 扩展了键值编码协议,允许通过键路径获取和设置层的 CATransform3D 矩阵的公共值。表 4 描述了层的 transform 和 sublayerTransform 属性是键值编码和观察兼容的关键路径

    【讨论】:

      猜你喜欢
      • 2015-04-18
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 2014-10-28
      相关资源
      最近更新 更多