Animation in Objective-c

A while back I was doing some native Objective C work for an iPhone app I was working on and I wanted to use sprite sheets to cut down on the amount of memory I was using. Memory use on iPhones is important, the standard native methods of animation are big memory hogs. Apple does not provide classes to read in sprite sheets, so I wrote one myself. Now this uses CALayers which is the code below UIKit. If you do not know anything about CALayer this code will seem complex stay with it… it does make sense.

You can download a zip of the required class here. You can also download a set of class here that use automatic reference counting which is new to iOS 5. I wrote these classes while working with iOS 4.3. Personally I prefer to do my own reference counting, but to each his own. From a suggestion from one of my peers I have added my code to github.

One very important thing is how you create your sprite sheet. I use TexturePacker. You also need to generate a plist that references information of where things are used in the sprite sheet. TexturePacker will do this for you. It is also very important that the names of the .plist file and .png of the sprite sheet be the same. I also use Flash to generate all my animations so all the individual files used to make up the sprite sheet need to be the same name of the sprite sheet with 000X. Here are the file names you will need:
“imagename”0001.png
“imagename”0002.png
….
“imagename”XXXX.png
these files will be used to make the sprite sheet and the plist:
“imagename”.png –> sprite sheet
“imagename”.plist –> plist file
You will need to add the sprite sheet and plist file to your project. I do not support rotations inside TexturePacker.

If you want to use the classes here are public functions:
-(id)initWithFile:(NSString *)fileName;
1. fileName is the file that contains the spite sheet and plist do not put the extension

-(id)initWithFile:(NSString *)fileName atLocation:(CGPoint)_location sizeInHalf:(BOOL)_sizeInHalf;
1. fileName is the file that contains the spite sheet and plist do not put the extension
2. _location this is the location of the where the animation will sit on the stage. Top left coordinate
3. _sizeInHalf this is a boolean that will cut the size of the animation in half

-(void)startAnimation:(int)frames repeatCount:(int)_repeatCount withReverse:(BOOL)isReverse withKey:(NSString *)key;
1. frame is the number of frames in the animation
2. _repeatCount is the number of times you would like the animation to repeat
3. isReverse is a boolean if set to YES the animation will run forward then backwards. If set to NO it will run through then start at the beginning again.
4. key is the key that will identify the animation, this can be any string.

-(void)removeMyAnimations:(NSString *)_key;
1. key is the key that will identify the animation, this can be any string.

-(void)startMovementAnimation:(int)frames repeatCount:(int)_repeatCount movement:(int)_movement withReverse:(BOOL)isReverse isNegative:(BOOL)_isNegative withKey:(NSString *)key;
1. frame is the number of frames in the animation
2. _repeatCount is the number of times you would like the animation to repeat
3. _movement this will set a movement of the animation in pixels
4. isReverse is a boolean if set to YES the animation will run forward then backwards. If set to NO it will run through then start at the beginning again.
5. IsNegative if this value is YES the movement will be in the negative direction. If YES the movement will be in the positive direction.
6. key is the key that will identify the animation, this can be any string.

-(void)startAnimation:(int)frames repeatCount:(int)_repeatCount atLocation:(CGPoint)_location withKey:(NSString *)key;
1. frame is the number of frames in the animation
2. _repeatCount is the number of times you would like the animation to repeat
3. _location is the location of the animation, Top left coordinate
4. key is the key that will identify the animation, this can be any string.
Note: I do not really use the above function since the location is added in the init function