Monday, February 21, 2011

UIImageView animation with stop on final frame

I'm using UIImageView's animationImages to show some frames over a second. I want to show the animation then have it rest on the final frame. In OS 3.1.3, this worked fine for that:

[self setAnimationRepeatCount:1];
[self setAnimationDuration:1];
[self startAnimating];
// auto set the last image in the frame
[self setImage:[UIImage imageNamed:[NSString stringWithFormat:@"frame-%d_29.png", self.frameSet]]];

When the animation was done it would display the image. No such luck with OS 4. I've tried setting an NSTimer and setting the static frame when that finishes, but there's a noticable flicker sometimes with that method. Any solution?

From stackoverflow
  • Edit

    Even better than the previous solution is simply to switch the order of the start and set:

    [self setImage:[UIImage imageNamed:[NSString stringWithFormat:@"frame-%d_29.png", self.frameSet]]];
    [self startAnimating];
    

    After messing around a bit more, I found that the NSTimer solution actually works if you set the delay to 29/30ths of a second, i.e. ~0.97:

    - (void)doAnimation
    {
        [self setAnimationRepeatCount:1];
        [self setAnimationDuration:1];
        [self startAnimating];
        [NSTimer scheduledTimerWithTimeInterval:0.97f
                                         target:self
                                       selector:@selector(onTimer:)
                                       userInfo:nil
                                        repeats:NO];
    }
    
    - (void)onTimer:(NSTimer *)theTimer
    {
        [self stopAnimating];
        [self setImage:[UIImage imageNamed:[NSString stringWithFormat:@"frame-%d_29.png", self.frameSet]]];
    }
    

0 comments:

Post a Comment