Get just the scaling transformation out of CGAffineTransform

前端 未结 7 1101
北荒
北荒 2021-02-01 19:26

I found a similar question about getting just the rotation, but as I understand scaling and rotating work different in the transform matrix.

Matrixes are not my strength

7条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-01 19:52

    Swift 4 Extension (thanks to Robin Macharg's answer)

    extension CGAffineTransform
    {
        var xScale: CGFloat { return sqrt(a * a + c * c) }
        var yScale: CGFloat { return sqrt(b * b + d * d) }
        var rotation: CGFloat { return CGFloat(atan2(Double(b), Double(a))) }
        var xOffset: CGFloat { return tx }
        var yOffset: CGFloat { return ty }
    }
    

    Note that CGFloat equals a Double on most platforms, thus conversions need to be to Double.

提交回复
热议问题