Search code examples
iphonecocoa-touchuikitcore-animationtransform

Bad performance with core animation and view rotations: How to improve?


I have 12 views which I rotate almost similar to the icons in the home screen when going into homescreen-customization-mode by touching an icon for a few seconds.

I use a 3D transformation code to do the rotations. They have each a duration of just 0.02 seconds, and the angle and direction of rotation changes permanently upon acceleration measurements.

5 of those views are fine, but 12 have a very bad performance. Here's the rotation code:

CATransform3D rotatedTransform = CATransform3DRotate(CATransform3DIdentity, degrees * M_PI / 180.0, 0.0f, 0.0f, 1.0f);
self.layer.transform = rotatedTransform;

there's some core animation wrapped around it. But I see a waste of CPU here, since I don't do any 3D stuff. I really only rotate 2D and nothing else. They're simple UIImageViews, but a custom class that does this rotation animations on her own.

I've heared that there are "affine" transforms which are supposed to work for the 2D space. Would you advise to change that to 2D in this case?

Must I prefer to change the transform of the layer, or of the view? which is faster?


Solution

  • CALayer rotations are hardware accelerated, 2D and 3D transforms would be pretty similar, no redraws are happening, no need to go to opengl, no need to cache.

    Shark would help, but you would need to know what you are looking for, and what you're looking for is not improving the performance of your current code, but removing your code completely.

    Sounds like you are going through your own code waaay too often, for the purpose of changing the angle of each layer. You win when you let CA do the work.

    So the strategy here is to find a way to have CA do the work, and call you back less than 50 times a second.

    How about a CAAnimationGroup containing a few CAPropertyAnimation for each of your layers and setting up, say, one second of animation at a time?

    Once you hand that off to CA, CA will playing it back in its own thread.