Tulon
Tulon

Reputation: 4124

Image sequence animation with multiple animationDuration iOS

I can load some UIImage from an array in an UIImaheView with animationImages & startAnimating properties, with a fixed animationDuration.

Like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.imageNamesArray = [[NSMutableArray alloc] initWithObjects:@"0001.png", @"0002.png", @"0003.png", @"0004.png", @"0005.png", @"0006.png", @"0007.png", @"0008.png", @"0009.png", @"0010.png", @"0011.png", @"0012.png", @"0013.png", @"0014.png", @"0015.png", @"0016.png", @"0017.png", @"0018.png", @"0019.png", @"0020.png", nil];


    self.imageArray = [[NSMutableArray alloc] init];

    for (int i = 0; i < [imageNamesArray count]; i++)
    {
        [imageArray addObject:[UIImage imageNamed:[imageNamesArray objectAtIndex:i]]];
    }

    self.imageView.animationImages = imageArray;
    self.imageView.animationDuration = 0.04;
    self.imageView.animationRepeatCount = 0;
    [imageView startAnimating];
}

But, I want to load those UIImage in that same UIImageView with different animationDuration. Let me explain what I mean. I have an array named imageNamesArray which contains 20 different image names. Now, I want to have an another array which will contain 20 different time duration.

self.durationArray = [[NSMutableArray alloc] initWithObjects:@"0.5", @"0.4",  @"0.4",  @"0.5",  @"0.4",  @"0.9",  @"0.4",  @"0.4",  @"0.4",  @"0.4",  @"0.4",  @"0.4",  @"0.4",  @"0.6",  @"0.4",  @"0.4",  @"0.8",  @"0.4",  @"0.4",  @"0.3",  @"0.4", nil];

And I want to show each image from that imageArray in that UIImageView according to the time duration of durationArray. How can I implement that. Can I use NSTimer to do the job? If any have solution please share with me. A lot of thanks in advance.

Upvotes: 2

Views: 728

Answers (1)

DilumN
DilumN

Reputation: 2897

Try this code, I wrote it just now, if there are errors please let me know.

-(void)refreshTimer
{
    self.imageView.image = [UIImage imageNamed:[self.imageNamesArray objectAtIndex:count]];
    [self performSelector:@selector(refreshTimer) withObject:nil afterDelay:[[self.durationArray objectAtIndex:count] doubleValue]];
    count++;
}

count here is an int. From your Viewdidload call this method once

[self refreshTimer];

Upvotes: 1

Related Questions