How does one make a UIImage Animation stop on an image.

Discussion in 'iOS Programming' started by nickculbertson, Nov 19, 2010.

  1. nickculbertson, Nov 19, 2010
    Last edited by a moderator: Jan 10, 2011

    nickculbertson macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #1
    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
     
  2. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #2
    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
    }
    
     
  3. nickculbertson thread starter macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #3
    Thanks for the quick reply. I let you know if I can get it to work.
     
  4. nickculbertson thread starter macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #4
    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]; 
    }
    
     

Share This Page