How to get the actual size of transformed CALayer?

2019-04-07 03:01发布

问题:

I have a 200x200 pixel CALayer instance to which I've applied two transformations:

  • scaled down to 50%
  • applied a 3D perspective

Now I want to align the transformed layer with another layer. To do that I need the size (width and height) of the transformed layer.

The problem is that layer.bounds still returns 200x200 for the layer, but because of the perspective transformation the actual visible width is less and the height more than 200. The bounds of the presentation layer return 0, by the way.

Is there any way how I can determine the exact size of a transformed CALayer exactly as it is drawn on screen?

Thanks, Mark.

回答1:

Finally figured it out: the bounds remain unchanged when applying a transformation, but what changes is the layer's frame property.

Solution is to use layer.frame.size instead of layer.bounds.size.

This is mentioned in the Core Animation Programming Guide:

Layers have an implicit frame that is a function of the position, bounds, anchorPoint, and transform properties.