How to know the current scale of a UIView?

Viewed 16505

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

6 Answers

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
Related