Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nickculbertson

macrumors regular
Original poster
Nov 19, 2010
226
0
Nashville, TN
Hello,
I'm trying to have a UIImage animation play when the app opens and stop on the last .png image. I'm using this to make a background do an animation then rest as a set image for the background home screen.

Here are the two methods I've tried, and where I need help:

Code:
- (void)viewDidLoad {
	animationTimer = [NSTimer scheduledTimerWithTimeInterval: (.2/2.0) target:self selector:@selector(tick) userInfo:nil repeats:YES];
	
    [super viewDidLoad];
}


- (void)tick{
	[self animateImages];
}

- (void)animateImages{
	
	UIImage *Image1 = [UIImage imageNamed:@"dm1.png"];
	UIImage *Image2 = [UIImage imageNamed:@"dm2.png"];
	UIImage *Image3 = [UIImage imageNamed:@"dm3.png"];
	UIImage *Image4 = [UIImage imageNamed:@"dm4.png"];
	UIImage *Image5 = [UIImage imageNamed:@"dm5.png"];
	UIImage *Image6 = [UIImage imageNamed:@"dm6.png"];
	UIImage *Image7 = [UIImage imageNamed:@"dm7.png"];
	UIImage *Image8 = [UIImage imageNamed:@"dm.png"];
	
	
	if(images.image == Image1)
		images.image = Image2;
	else if(images.image == Image2)
		images.image = Image3;
	else if(images.image == Image3)
		images.image = Image4;
	else if(images.image == Image4)
		images.image = Image5;
	else if(images.image == Image5)
		images.image = Image6;
	else if(images.image == Image6)
		images.image = Image7;
	else if(images.image == Image7)
		images.image = Image8;
	else 
		images.image = Image1;	
}

With this method the image animation plays from launch but loops forever. I tried changing repeats:NO but it just stops the first image.

second method:

Code:
- (IBAction)start: (id)sender {
	
	color.animationImages = [NSArray arrayWithObjects:
							 [UIImage imageNamed:@"dm.png"],
							 [UIImage imageNamed:@"0cfb68c1b2.png"],
							 [UIImage imageNamed:@"dm.png"],
							 [UIImage imageNamed:@"0cfb68c1b2.png"],
							 [UIImage imageNamed:@"dm.png"],
							 [UIImage imageNamed:@"0cfb68c1b2.png"], nil];
	
	[color setAnimationRepeatCount:2];
	color.animationDuration = 4;
	[color startAnimating];
}
with this method I must click a button for the animation to start but the loop is controlled.

Thanks for any advice you can throw my way.
Nick
 
Last edited by a moderator:
You would probably have to setup yourself as the stop animation delegate and then implement a selector:

Code:
[myImageView setAnimationDelegate:self];
[myImageView setAnimationDidStopSelector:@selector(animationDone:)];

and then

Code:
- (void) animationDone: (id) sender {
//Do something
}
 
Thanks again. Here is the code I used (left out NSTimer altogether).

Code:
- (void)viewDidLoad {
	image.animationImages = [NSArray arrayWithObjects:
							 [UIImage imageNamed:@"dm.png"],
							 [UIImage imageNamed:@"0cfb68c1b2.png"],
							 [UIImage imageNamed:@"dm.png"],
							 [UIImage imageNamed:@"0cfb68c1b2.png"],
							 [UIImage imageNamed:@"dm.png"],
							 [UIImage imageNamed:@"0cfb68c1b2.png"], nil];
	
	[image setAnimationRepeatCount:1];
	image.animationDuration = 4;
	[image startAnimating]; 
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.