22

How do you find the current scale (zoom level) of a UIView?

shim
  • 9,289
  • 12
  • 69
  • 108
Jonathan
  • 873
  • 3
  • 9
  • 23

6 Answers6

53

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;
Tim
  • 59,527
  • 19
  • 156
  • 165
  • 2
    I 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
  • 2
    As 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
13

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.

Vodenjak
  • 806
  • 1
  • 13
  • 21
3

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
Vladimirs Matusevics
  • 1,092
  • 12
  • 21
  • 1
    While 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
1

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.

dans3itz
  • 1,605
  • 14
  • 12
1

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

Community
  • 1
  • 1
TheTiger
  • 13,264
  • 3
  • 57
  • 82
0

Isn't that the contentScaleFactor (reference)?

trojanfoe
  • 120,358
  • 21
  • 212
  • 242