CGAffineTransform not scaling origin correctly

656 Views Asked by At

I may be misunderstanding how the CGAffineTransform works but it seems to be giving strange results for the origin of the frame.

for example :

        print(attribute.frame)
        attribute.transform = CGAffineTransform(scaleX: 0.68, y: 0.68)
        print(attribute.frame)

gives the results :

(213.0, 54.0, 459.0, 23.5)

(286.29948979591836, 57.75280612244898, 312.4010204081633, 15.994387755102032)

The width and height scale correctly but the x and y origin have increased in value.

2

There are 2 best solutions below

1
Frank Cheng On

I thinks the transform must be apply to the center of rect.
I don't know what is the type of attribute. Maybe there is something called anchor inside attribute. You can try to change the property.

4
SveinnV On

The transform uses the center of your view as anchor point. The result being that the center stays the same, should be (442.5, 65,75) if i calculate correctly but the origin will move (increase in value if you scale down, and decrease if you scale up). There are various techniques to change the anchor point if you want to keep the origin, perhaps take a look at this thread : Scale with CGAffineTransform and set the anchor