views:

99

answers:

2

hi,

I have an image that is animating, until you press the button that says "Stop". The code works and all, but the image returns to the first image in the array. How can i add a code to tell it to stop at the image it's currently at?

- (void)viewDidLoad {

    [super viewDidLoad];

    imageView.animationImages = [NSArray arrayWithObjects:
                             [UIImage imageNamed:@"a0001.png"],
                             [UIImage imageNamed:@"a0002.png"],
                             [UIImage imageNamed:@"a0003.png"],
                             [UIImage imageNamed:@"a0004.png"],
                             [UIImage imageNamed:@"a0005.png"], nil];

    imageView.animationDuration = 3.00;
    imageView.animationRepeatCount = 0;
    [imageView startAnimating];
    [self.view addSubview:imageView];   
}

- (IBAction)stopAni {       
    [imageView stopAnimating];      
}

Thanks

+1  A: 

Set the image property of the UIImageView to be the one you want to stop at

- (IBAction)stopAni {
    [imageView stopAnimating];
    [imageView setImage:[[imageView animationImages] objectAtIndex:currentFrame];
}

The current frame is just the animation duration divided by the amount of time that you have been animatin (you'll have to store the time that you started animating yourself!)

deanWombourne
Thank deanWombourne :D
okayasu
hey but how to store the time I started the animation?
okayasu
Something like __startTime = [[NSDate date] timeIntervalSince1970];__ should give you a reference time (as a NSTimeInterval). You can get the duration of the animation by __NSTimeInterval duration = [[NSDate date] timeIntervalSince1970] - startTime;__
deanWombourne
thank you :D i'm a newbie :D
okayasu
A: 

Thanks dean, hey I need you once again :p now I've been able to stop it where I want, can I make it continue the animation where it has stopped??

okayasu
Trickier - you would have to set a new animationImages array starting from where you left off last time.
deanWombourne
[[imageView startAnimating] objectAtIndex:currentFrame];can this be done? I tried but I got an error :s
okayasu
You can't do that - startAnimating doesn't return an NSArray so objectAtIndex won't work :)If you post this answer as a new question people can probably help you more ;)
deanWombourne