Is there a way to figure out, how many degrees an view is rotated currently during an animation?

前端 未结 3 1386
被撕碎了的回忆
被撕碎了的回忆 2020-12-03 04:08

I am applying an rotation transform animation in an animation block with this transform:

CATransform3D rotatedTransform = self.layer.transform;
rotatedTransf         


        
3条回答
  •  猫巷女王i
    2020-12-03 04:36

    You can retrieve the current state of an animating layer by grabbing its presentationLayer. Assuming that you have not applied any other transformations to the layer, you can extract the current angle of the rotating layer using code like the following:

    CATransform3D rotationTransform = [(CALayer *)[self.layer presentationLayer] transform];
    float angle;
    if (rotationTransform.m11 < 0.0f)
        angle = 180.0f - (asin(rotationTransform.m12) * 180.0f / M_PI);
    else
        angle = asin(rotationTransform.m12) * 180.0f / M_PI;
    

    In an otherwise unmodified transform, the m11 and m12 values are coordinates that lie on the unit circle, so you can use trigonometry to determine the angle they describe.

    EDIT (5/18/2009): Added a typecast to CALayer to overcome compiler warnings and fixed the naming of the transform in the trigonometry operations.

提交回复
热议问题