iPhoneSDK calculate Rotation angle from CATransform3D

这一生的挚爱 提交于 2019-12-18 10:24:10

问题


How do I calculate the Rotation in Radians around Z-axis by giving a CATransform3D struct as the input?

basically what I need is the other way round of CATransform3DMakeRotation.


回答1:


It depends on what axis you are doing the rotation on.

Rotation about the z-axis is represented as:

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)

so angle should be a = atan2(transform.m12, transform.m11);

Rotation about x-axis:

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)

Rotation about y-axis:

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) 



回答2:


If the transform is attached to a layer, then you can get the value of the rotation like follows:

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

From the documentation:

Core Animation extends the key-value coding protocol to allow getting and setting of the common values of a layer's CATransform3D matrix through key paths. Table 4 describes the key paths for which a layer’s transform and sublayerTransform properties are key-value coding and observing compliant



来源:https://stackoverflow.com/questions/3565383/iphonesdk-calculate-rotation-angle-from-catransform3d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!