Combining UIView Animation Blocks and OpenGL ES Rendering

I am developing an iP * game and I am using UIKit and OpenGL ES 2.0. UIKit elements are displayed on top of the OpenGL view and occupy a significant (arbitrary) amount of screen space. I must admit that Apple did an excellent job, and the frame rate in the game is always 60 FPS.

To come to this conclusion, I did a lot of tests regarding performance:

  • I added a lot of static (non-moving) UIViews over the OpenGL view - OK!
  • I animated the same UIViews using custom code (I changed the center property in the drawFrame method of my game) - OK !.
  • I have added many OpenGL ES elements under UIViews - OK!
  • I added a lot of moving elements of OpenGL ES under UIViews - OK!

So now I want to use the Core Animation Framework to animate UIKit elements. I use

[UIView animateWithDuration:delay:options:animations:completion:] 

to complete the animation (I am targeting iOS 4 or later).

The problem is that the frame rate has this <strong> strange behavior: sometimes I get 60 frames per second with many many animated UIKit elements (30 elements are ok for me) and in some cases the frame rate is obviously lower than 60 frames per second even with a single animated UIKit element, but I can't measure it with the tools! Let me explain in more detail: when I open the Tools and control the main animation and / or the OpenGL driver, I always get 60 frames per second. But obviously this is not the case, I can see that OpenGL animations can move much slower than the corresponding UIKit animations from above. If I remove the UIKit elements from the view, the frame rate will return to normal. A similar situation with the one I describe here occurs in any OpenGL ES game, when the user changes the volume of the device during the game. When the transparent view showing the current volume starts to disappear, and until it completely disappears, the frame rate drops sharply, but in the tools (I also did this test) it stuck at 60 frames per second!

So to summarize: sometimes I get real 60 frames per second with block animations without ups and downs to run, and sometimes I get fake 60 frames per second without ups and downs.

Do you have any solution? All tests were conducted on the iPad 2 and iPhone 3GS with iOS 5.1.

+8
iphone uikit animation opengl-es quartz-graphics
source share
1 answer

I managed to solve the problem, and now I can combine UIView block / kernel based animation in my OpenGL game without sacrificing performance.

The solution is pretty simple:

  • For each UIView that you want to use in your OpenGL application, save its borders, center and transform the properties in your OpenGL screen coordinate system (for example, create the following properties: GLBounds, GLCenter, GLTranform) and update them accordingly when you change one of corresponding properties of UIView.
  • When you start the game, download UIViews, but set them hidden . Thus, UIKit does not draw on top of the OpenGL view (thus, the frame problem drops completely eliminated). Instead, create your own UIViews yourself using the GL * properties created in step 1 and using the appropriate images (the images / colors you used for each UIView).
  • Animate the hidden UIViews properties (borders, center, and transform) using block / core animation depending on the animation you want to achieve (which in turn updates your GL * properties) and in your DrawGL draw method uses GL * properties to drawing UIViews! To get the actual values โ€‹โ€‹for the borders, center and transform when the UIView animates (and updates the GL * properties), use the presentationLayer property.

Best

+9
source share

All Articles