Reputation: 31
I know that I can change the size of a CALayer instance by modifying both the bounds
and the transform
properties.
But, which one has better performance? Or is there any principle about how to pick the suitable property to use?
Upvotes: 0
Views: 898
Reputation: 170317
These are two different properties in the way that they interact with the CALayer, so I don't treat them as being interchangeable.
When you change a transform on a CALayer, you are taking the base content and geometry of that layer and transforming it in such a way that you can scale, rotate, apply perspective, or do other 3-D effects to it. These are all hardware accelerated, because you're effectively applying an OpenGL (ES) transformation matrix to the 2-D rectangular texture of the CALayer. Note that applying a transform does not cause the CALayer content to be re-rendered, so you can end up with blurry graphics and text if you scale a layer up or down.
If you alter the bounds of a CALayer, you are deforming the layer itself by directly adjusting its geometry. By default, this is hardware accelerated. However, this can end up being much more expensive if you set the needsDisplayOnBoundsChange
property of a CALayer to YES. In that case, the layer will be re-rendered at the new size if you change its bounds, which can be a slow operation. You'd use this option if you wished to have crisp graphics at the end of your resizing action, something a transform won't provide.
In summary, while scaling transforms and bounds changes by default can be roughly the same in terms of performance, the latter can be made much slower with the right settings. In general, my recommendation is to manipulate the geometry of a layer directly if you wish to move it or resize it and have it stay there, with transforms reserved for quick animations, like bounces, shakes, or pop-outs, or for the application of more advanced manipulations like perspective effects.
Upvotes: 3