How do you "ping pong" animate a UIView image sequence

I have a UIImageView that has a sequence of PNG images loaded.

My question is, did you know that I can "ping pong" in an animated sequence? So that he plays forward from 1-24, then he plays backward from 24-1 and cycles.

(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 you so much!

+2


source to share


2 answers


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.

+7


source


No, everything in Cocoa is counted by reference, so an object's membership in multiple collections will only have one instance if you don't use a copy.



0


source







All Articles