tags:

views:

4138

answers:

3

Hi,

I'm trying to animate some UIImageViews for a 2D game. Using [UiView begin/commit animation] is out of the question, since I need to determine the positions of all Image Views at all times. Instead I'm changing the frame.origin.x/y in my UIImageViews to manualy control my animations. My problem is that the framerate drops significantly for high numbers of UIImageViews or if their size is too large (btw, the UIImageViews contain PNGs with transparency colours). Do you have any optimization advices?

Thanks

A: 

For a 2D game I would go with Cocos2D-iphone.

It's a lot like Flash, and has the benefit of being open-source and has tight integration with Chipmunk Physics. Also, is under excellent and great development by riq.

You can do a lot more with Cocos than just games as well. Definitely worth checking out.

Genericrich
+6  A: 

UIImageView is just fine for animating about the display (UIViews are lightweight wrappers around CALayers, which are just textures stored on the graphics card), but you do need to use the begin / commit animation block to get smooth animation. Using that, you can retrieve the current position of the view by accessing the backing layer of the UIView and obtaining its presentation layer. Dustin Bachrach has a post that describes just how to do this. Basically, the presentation layer is Core Animation's closest approximation to where the actual layer is at any given time, so its frame is the frame of your UIView at that instant.

I'm not sure that touch events will find their way to the view as it's moving across the screen, so if you need to have the UIImageView respond to touches you may need to replace it with a CALayer, insert it as a sublayer of your main UIView's layer, and call the hitTest method on the presentationLayer of that main layer. That should return the presentationLayer of your moving layer if it was clicked on, or the background layer if not.

Honestly, I wouldn't go with OpenGL unless you absolutely had to for performance reasons, because it is a lot harder to code for than what Core Animation provides you with UIViews and CALayers. I've been able to animate 50 moving translucent layers at 60 FPS (100 at 30 FPS) using just UIViews and CALayers.

Brad Larson
Thanks for the hint. I eventualy managed to get the UIView frame at "animation time".
MihaiD
A: 

OK, using begin/commit animation solved my framerate problem and I was able to determine the real time coordinates of my UIView. One final question remains: can I programatically stop such an animation?

MihaiD
This is a new question and shouldn't be asked here really. I found this because I was searching for the same question before posting it myself. See http://stackoverflow.com/questions/554997/cancel-a-uiview-animation
Phil Nash