Search code examples
scalecgaffinetransform

CGAffineTransformscale formula when size is known


I have to animate a UIImageView from a certain large width and height to a known smaller width and height in a given number of steps of time eg 10 steps or 20. I am using CGAffineTransformscale, but am unable to figure how how to compute the steps by which the individual downscales should happen. Is there a formula that you can share with me?

Thanks in advance,


Solution

  • At last, a practical use for logarithms.

    Remember that

    log(X^Y) = log(X) * Y
    

    So

    log(X) = log(X^Y) / Y
    

    So

    X = exp(log(X^Y) / Y)
    

    In this case, X is the scale to make at each step. Y is the number of steps. X^Y is the total scale you want to make. For example,

    Steps = 10;
    TotalScale = 0.5;
    ScaleEachTime = exp(log(TotalScale) / Steps);
    

    gives (dots... because I am rounding off in the example)

                  = exp(log(0.5) / 10)
                  = exp(-0.693... / 10)
                  = exp(-0.0693...)
                  = 0.9330....
    

    So scaling 10 times by 0.9330... gives a total scale of 0.5