iosobjective-ccore-animationcatransform3dcatransform3drotate

CATransform3D: Calculate height after perspective transform


I have a layer with a height of 50, and I'm rotating it about the x axis. . . How can I calculate the height as the rotation proceeds?

CATransform3D subLayerTransform = CATransform3DMakeTranslation(0, 0, 0);
subLayerTransform.m34 = -1 / 1800; //How does height relate to perspective and angle?
subLayerTransform = CATransform3DTranslate(subLayerTransform, 0, 0, 0);
subLayerTransform = CATransform3DRotate(subLayerTransform, 45 * (M_PI / 180), 1, 0, 0);
_transitionLayer.sublayerTransform = subLayerTransform;

Solution

  • To rotate about the y axis

    And the height is y1 - y0. Therefor to calculate the new y1 and y0:

    CGFloat y0 = 0;
    CGFloat y1 = 50;
    CGFloat z = -1800; //This relates to the m34 perspective matrix.
    
    y0 = y0 * cos(radians(45)) - z * sin(radians(45));
    y1 = y1 * cos(radians(45)) - z * sin(radians(45));
    
    CGFloat newHeight = y1 - y0;