How to know the current scale of a UIView?

后端 未结 6 962
一整个雨季
一整个雨季 2020-12-16 12:16

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

相关标签:
6条回答
  • 2020-12-16 12:34

    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;
    
    0 讨论(0)
  • 2020-12-16 12:34

    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.

    0 讨论(0)
  • 2020-12-16 12: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.

    0 讨论(0)
  • 2020-12-16 12:41

    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
    
    0 讨论(0)
  • 2020-12-16 12:46

    Isn't that the contentScaleFactor (reference)?

    0 讨论(0)
  • 2020-12-16 12:49

    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

    0 讨论(0)
提交回复
热议问题