views:

457

answers:

2

Hi,

I have a UIImageView which has a png image sequence loaded into it.

My question is - Do you know of any way I can "ping pong" the animation sequence? So that it plays forward from 1-24 then plays backwards from 24-1 and loops.

(technically it should be : 1-24 then 23-1 then 2-24 then 23-1...etc)

- (void) loadAnim01 {
mon01 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mon01_01.png"]];
mon01.center = CGPointMake(258,69);
NSMutableArray *array = [NSMutableArray array];
for (int i = 1; i <= 24; i++) 
 [array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"mon01_%02d.png",i]]];
mon01.animationImages = array;
mon01.animationDuration = 1.0;
mon01.animationRepeatCount = 0;
[self.view addSubview:mon01];
[mon01 release];

}

Thank a lot!

+4  A: 
for (int i = 1; i <= 24; i++) {
    [array addObject:[UIImage imageNamed:[NSString stringWithFormat:@"mon01_%02d.png",i]]];
}
for (int j = 23; j >= 2; j--) {
    [array addObject:[array objectAtIndex:j-1]];  // -1 since arrays are 0-based
}

this adds a second copy of all but the first and last animation, in reverse order, which should give you a ping-pong effect.

David Maymudes
Done!Thank you!
Jonathan
remember to click the check-mark to accept the answer...
David Maymudes
A: 

Doesn't adding the objects into the array twice mean that it's using up twice the memory?

Joe