Behavior of CGAffineTransformMakeScale with CGREct and with UIView

I have a view with a frame defined as (0,0,320,480).
I invoke the conversion on this view:

self.myView.transform = CGAffineTransformMakeScale(factor, factor); 

The view will be scaled, keeping the center position on the screen, and its frame after my changes will be, for example, (34, -8,251,376), since you can see that X and Y are now different from 0.

If I use the same function on a CGRect with a frame (0,0,320,480):

 CGAffineTransform t = CGAffineTransformMakeScale(factor,factor); CGRect rect2 = CGRectApplyAffineTransform(rect,t); 

rect2 will save 0 for X and Y, and I will end up with something like (0,0,251,376)

Why are X and Y for rect2 not changing, as in the UIView example?

+4
source share
2 answers

It is true that technically you should not look at the frame UIView property after the conversion, but it also does not have the technically relevant question that you are asking.

When applying CAffineTransforms to a UIView, the transformation takes into account the CALayer binding property for the UIView. From CALayer docs to anchorPoint:

By default (0.5, 0.5), the center of the rectangle is the borders.

This means that when applying that scale to the transformation, it uses the center of view as the anchor point, so the view scales around this place. I assume that if you set the anchor point (0, 0), it will behave like a CGRect.

CGRect, on the other hand, is a simple C structure, and does not have a protective layer or nodal point. So the difference in behavior.

+7
source

The link to the UIView page says specifically:

A warning. If the transform property is not an identical transformation, the value of this property is undefined and therefore should be ignored.

So do not look at the view frame after converting it.

+1
source

Source: https://habr.com/ru/post/1344007/


All Articles