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

- 13,044
- 6
- 50
- 73

- 2,910
- 3
- 14
- 22
3 Answers
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;

- 4,214
- 3
- 36
- 55
-
7I wonder why the CGAffineTransform class doesn't have properties for this kind of stuff? – bcause Jun 22 '17 at 08:57
I had the same problem, found this solution, but it only partially solved my problem. In fact the proposed solution for extracting the scale from the transform:
(all code in swift)
scaleX = view.transform.a
scaleY = view.transform.d
only works when the rotation is 0
.
When the rotation is not 0
the transform.a
and transform.d
are influenced by the rotation. To get the proper values you can use
scaleX = sqrt(pow(transform.a, 2) + pow(transform.c, 2))
scaleY = sqrt(pow(transform.b, 2) + pow(transform.d, 2))
note that the result is always positive. If you are also interested in the sign of the scaling (the view is flipped), then the sign of the scaling is the sign of transform.a
for x flip and transform.d
for y flip. One way to inherit the sign.
scaleX = (transform.a/abs(transform.a)) * sqrt(pow(transform.a, 2) + pow(transform.c, 2))
scaleY = (transform.d/abs(transform.d)) * sqrt(pow(transform.b, 2) + pow(transform.d, 2))

- 461
- 5
- 9
In Swift 3:
let rotation = atan2(view.transform.b, view.transform.a)

- 5,514
- 5
- 28
- 38
-
You don't need to convert the argument types — the standard library provides overloads for `atan2` that take `CGFloat`, `Double`, or `Float`. – rickster Jan 09 '18 at 22:53
-
I get an error: "Cannot convert value of type 'CGFloat' to expected argument type 'Float'" – cleverbit Jan 09 '18 at 22:58
-
```let rotation = atan2(view.transform.b, view.transform.a)``` works! – cleverbit Jan 09 '18 at 23:00