문제

I have an iOS App that loads a set of images and a duration into an array then I have a timer that displays the images like so:

- (void)fireTimer:(NSTimer *)theTimer
{
    self.image = [frames objectAtIndex:currentFrame];
    NSTimeInterval time = [[durations objectAtIndex:currentFrame] floatValue];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(fireTimer:) userInfo:nil repeats:NO];
    currentFrame++;
    if (currentFrame >= [frames count])
    {
        currentFrame = 0;
        [timer invalidate];
    }
}

To start the animation I call fireTimer and cycle through the images then when all the images are processed I call [timer invalidate] to stop the animation. I cannot use startAnimation because I need different durations for each image.

Right know I am NOT doing this on a background thread so the animation is choppy because other processing is happening whilst the image animates.

What is the best way to animate this in the background? Can I simply put this call to fireTimer in a block?

I know this may not be the best way to animate an image on iOS, but I do not want to do a lot of refactoring on the code right now.

Thanks for any suggestions or examples for a better solution!!!

도움이 되었습니까?

해결책

I would suggest you use UIImageView to animate your images : it is made to do this task. If, like you say, some images needs to remains longer then others, then you can just display them many times. Let's say that image2 needs to be displayed twice longer than image1 and image3, just initialize the array animationImages on your UIImageView like : @[image1, image2, image2, image3].

다른 팁

Instead of using a timer, you could use performSelector: withObject: afterDelay:

-(void)loopBackground:(int)index
{
    if(index < [self.durations count]{
        self.image = [self.frames objectAtIndex:index];
        [self performSelector:@selector(loopBackground:) withObject:index++ afterDelay:[[self.durations objectAtIndex:index] floatValue]];
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top