How do you find the current scale (zoom level) of a UIView
?
-
are you using scrollView for zooming ? – TheTiger Aug 20 '12 at 15:00
-
No, I use UIPinchGestureRecognizer and CGAffineTransformScale. – Jonathan Aug 20 '12 at 15:02
6 Answers
If you're applying a scale transform to your view, that transform will be available (appropriately enough) through the transform
property on UIView. According to the CGAffineTransform docs, scale transforms will have nonzero values at coordinates (1,1) and (2,2) in the transform matrix; you can therefore get your x- and y-scale factors by doing:
CGFloat xScale = view.transform.a;
CGFloat yScale = view.transform.d;

- 59,527
- 19
- 156
- 165
-
2I already tried but it did not work. I made a mistake somewhere in this case. Thanks for your response ! – Jonathan Aug 20 '12 at 15:14
-
2As a note here: It works well if the view was scaled only. However, it will got a wrong value when the view was also rotated. @Margaret 's answer works in this case then, i.e. `sqrt(Double(transform.a * transform.a + transform.c * transform.c))`. – Kjuly Oct 22 '19 at 08:35
According to this source, using this method gives you the scale regardless of rotation or translation applied to transform:
func scale(from transform: CGAffineTransform) -> Double {
return sqrt(Double(transform.a * transform.a + transform.c * transform.c));
}
I know I am late to the party but the accepted answer didn't work in my case.

- 806
- 1
- 13
- 21
Using some math around CGAffineTransform transformation matrix you can calculate a scale. This also mentioned in Margaret's comment. Swift allows you to write a simple extension with a computed property:
extension CGAffineTransform {
var scale: Double {
return sqrt(Double(a * a + c * c))
}
}
So, later you can use by calling a scale
directly on the CGAffineTransform
, like this:
let someViewScale = someView.transform.scale

- 1,092
- 12
- 21
-
1While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – IlGala Oct 01 '19 at 08:19
That functionality is provided with UIScrollView and it's zoomScale property.
EDIT:
Knowing the current scale is given by the transformation matrix. The scale values, as you know, are here:
sx, 0, 0
0 , sy, 0
. . .. ,1.
To get the current state, just record the transform's state. To return to that state, however, you'll need to use the inverse of your last transformation or load the identity matrix.

- 1,605
- 14
- 12
-
I know and I can't use UIScrollView for this project, not powerful enough =/ – Jonathan Aug 20 '12 at 15:06
Check this it will help you .... In this you can learn how to set minimum and maximum zoom scale using UIPinchGestureRecognizer
.... How to set minimum and maximum zoom scale using UIPinchGestureRecognizer
-
During the scale modification, this value doesn't change so I think it's not for this use. – Jonathan Aug 21 '12 at 08:41