Get just the scaling transformation out of CGAffineTransform

前端 未结 7 1106
北荒
北荒 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:50

    Let me propose different solution. I first rotate the affine transform in the opposite direction and just read scale from t.a and t.d:

    - (CGPoint)scaleFromTransform {
        CGAffineTransform t = self.transform;
        CGFloat angle = atan2(t.b, t.a);
        // calculate rotation transform (by -angle)
        CGAffineTransform r = CGAffineTransformMakeRotation(-angle);
        // calculate main transform without rotation
        CGAffineTransform t0 = CGAffineTransformConcat(t, r);
        CGFloat xScale = t0.a;
        CGFloat yScale = t0.d;
        return CGPointMake(xScale, yScale);
    }
    

提交回复
热议问题