tags:

views:

162

answers:

1

I have created CALayer objects and am able to animate their movement around the screen. However, now I want to animate them to change through a set of images in a loop to create an animation (like an animated gif)

I'm fairly new to programming and very new to cocoa so code examples welcomed.

I have 15 png images.

--Charliehorse

EDIT: I have code that creates an NSArray of 15 CGImageRef objects.

A: 

Have the object that owns the array and layer (I presume there is an object that owns both) also own a timer, which sends the object a message to change the image displayed in the layer. The same object should also have an instance variable containing an index into the array.

To respond to the timer message, check whether there are any images in the array, and, if so, divide the index by the count of the array and take the remainder (% operator). The result is the index to access; get the image from that index in the array and change the image in the layer, then add 1 to the computed index and assign it back to the variable.

Peter Hosey
Thanks. So CALayer will animate the changing of the sprite.contents property for CASublayers?
Charliehorse
I found it's much better to use CAKeyFrameAnimation with the array of CGImages.
Charliehorse
Charliehorse: Sounds interesting. Would you post a separate answer describing that solution in more detail, and accept that?
Peter Hosey