How do you find the current scale (zoom level) of a UIView
?
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;
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.
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.
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
Isn't that the contentScaleFactor
(reference)?
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