Search code examples
iosanimationrotationscaletransformation

Get scale and rotation angle from CGAffineTransform?


I want to get scale factor and rotation angle form view. I've already applied CGAffineTransform to that view.


Solution

  • The current transformation of an UIView is stored in its transform property. This is a CGAffineTransform structure, you can read more about that here: https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CGAffineTransform/Reference/reference.html

    You can get the angle in radians from the transform like this:

    CGFloat angle = atan2f(yourView.transform.b, yourView.transform.a);
    

    If you want the angle in degrees you need to convert it like this:

    angle = angle * (180 / M_PI);
    

    Get the scale like this:

    CGFloat scaleX = view.transform.a;
    CGFloat scaleY = view.transform.d;