How to know the current scale of a UIView?
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
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;