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

前端 未结 3 1365
被撕碎了的回忆
被撕碎了的回忆 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条回答
  • 2020-12-03 04:32

    Try this:

    CGFloat setAngle = 75.0;
    CATransform3D t = CATransform3DIdentity;
    t.m34 = 0.004;
    t = CATransform3DRotate(t, -M_PI/180*setAngle, 0, 1, 0);
    self.testView.layer.transform = t;
    
    CGFloat getAngle = 0;
    if (t.m11 < 0.0f) {
        getAngle = 180.0f - (asin(t.m13) * 180.0f / M_PI);
    } else {
        getAngle = asin(t.m13) * 180.0f / M_PI;
    }
    NSLog(@"setAngle=%@ getAngle=%@", @(setAngle), @(getAngle));
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-03 04:53

    This is an old question but I came across this answer while trying to find the current rotation angle during a key frame animation:-

    CALayer* layer = [self.layer presentationLayer];
    float currentAngle = [[layer valueForKeyPath:@"transform.rotation.z"] floatValue];
    

    Seems to work for me. Also, nice and concise.

    0 讨论(0)
提交回复
热议问题