Why do layer transforms affect the UIView frame?

Converting a UIView affects its frame. Converting a UIView layer also affects the structure of views in the same way. Thus, scaling the presentation level scales the frame. I am trying to understand why transformations to a layer affect the view frame (even if view.layer.masksToBounds = NO ).

 UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; NSLog(@"Before: %@", NSStringFromCGRect(view.frame)); // Output: {{0, 0}, {50, 50}} // View transform applied view.transform = CGAffineTransformMakeScale(2, 2); NSLog(@"%@", NSStringFromCGRect(view.frame)); // Output: {{-25, -25}, {100, 100}} // Layer transform applied view.transform = CGAffineTransformIdentity; view.layer.transform = CATransform3DMakeScale(2, 2, 1); NSLog(@"%@", NSStringFromCGRect(view.frame)); // Output: {{-25, -25}, {100, 100}} 
+5
source share
3 answers

You should not look at the value of the frame as soon as you have the transformation, since it is undefined what it contains at this point. This is mentioned in the documentation for the frame property in the UIView :

WARNING

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

If you need to change the frame, you must do this using the center and bounds properties instead.

+5
source

A frame is a very specific thing.

This rectangle determines the size and position of the view in its supervisor coordinate system. You use this rectangle during layout operations to size and position the view.

The transformations applied to the view affect the start and size of this view in supervision, so the frame of the view changes.

Converting subviews will affect subview frames, but not their supervisor frame.


It is worth noting that bounds differs from frame in this respect. The boundaries of the view are the beginning and size of the view within its own coordinate system. Transformations should not change the boundaries of the view, because the transformation changes the size and position of the view for external coordinates, but not the internal coordinates of the view.

+1
source

A frame is a computational property. In principle, it is synthesized from the center and boundaries. (To find out more, search for anchorPoint CALayer.) What else when transformation is taken into account. The frame will be the bounding box that will cover the original box, even when rotating or scaling. And the default implementation of hitTest and pointInside will use the final frame, which means that you can normally touch the translated or rotated view.

0
source

All Articles