stop the .png sequence animation

Discussion in 'iOS Programming' started by jjgraz, Sep 25, 2009.

  1. jjgraz macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #1
    Hi,

    I have two simple animations (png images running in sequence).
    Each animation has transparent backgound, so the background never changes.
    I have two buttons used to initiate the animations. It all works dandy, however I'm trying to make the app work so when one animation is running, and the second button is pushed, first animation stops. Second starts. Currently the first keeps running until finish. It needs to stop when second is intiated. Any thoughts would be greatly appreciated. Code sample below.

    Code:
    - (IBAction)buttonPressed1:(id)sender
    {
    	int x=2;
    	if (x=2){
    		NSLog(@"%i", x);
    		
    	UIImageView* aView = [[UIImageView alloc] initWithFrame:self.view.frame];
    	
    	aView.animationImages = [NSArray arrayWithObjects:	
    									
    		[UIImage imageNamed:@"01.png"],
    				.....(many more pngs)......
    	
    	aView.animationDuration = 2.00;
    	
    	
    	[aView startAnimating];
    	[self.view addSubview:aView];
    	[aView release]; 
    	}
    }
    
    - (IBAction)buttonPressed2:(id)sender
    {
    	int x=1;
    	if (x=1){
    		NSLog(@"%i", x);
    		
    	UIImageView* aView = [[UIImageView alloc] initWithFrame:self.view.frame];
    	
    	aView.animationImages = [NSArray arrayWithObjects:	
    									
    		[UIImage imageNamed:@"02.png"],
    			.....(many more pngs)......	
    	
    	aView.animationDuration = 2.00;
    	
    	
    	[aView startAnimating];
    	[self.view addSubview:aView];
    	[aView release]; 
    	}
    }
    
    
    Thanks a ton for any feedback.
     
  2. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #2
    Try calling two different names for views in your h file (aView, bView) and then declare in the second function [aView stopanimating] and vice versa.

    Code:
    - (IBAction)buttonPressed1:(id)sender
    {
    	int x=2;
    	if (x=2){
    		NSLog(@"%i", x);
    		
    	UIImageView* aView = [[UIImageView alloc] initWithFrame:self.view.frame];
    	
    	aView.animationImages = [NSArray arrayWithObjects:	
    									
    		[UIImage imageNamed:@"01.png"],
    				.....(many more pngs)......
    	
    	aView.animationDuration = 2.00;
    	
    	[bView stopAnimating];
    [bView removefromSuperview];
    	[aView startAnimating];
    	[self.view addSubview:aView];
    	//[aView release]; 
    	}
    }
    
    - (IBAction)buttonPressed2:(id)sender
    {
    	int x=1;
    	if (x=1){
    		NSLog(@"%i", x);
    		
    	UIImageView* bView = [[UIImageView alloc] initWithFrame:self.view.frame];
    	
    	bView.animationImages = [NSArray arrayWithObjects:	
    									
    		[UIImage imageNamed:@"02.png"],
    			.....(many more pngs)......	
    	
    	bView.animationDuration = 2.00;
    	
    	[aView stopAnimating];
    [aView removefromSuperview];
    	[bView startAnimating];
    	[self.view addSubview:bView];
    	//[bView release]; 
    	}
    }
    Tell me how that works out
     
  3. jjgraz thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #3
    Under your thought suggestion I now have,

    my h. file now looks like

    Code:
    
    #import <UIKit/UIKit.h>
    
    @interface MainViewController : UIViewController {
            UIImageView *aView;
    	UIImageView *bView;
    }
    - (IBAction)buttonPressed1:(id)sender;
    - (IBAction)buttonPressed2:(id)sender;
    
    @end
    
    
    So a local declaration of the views.

    now m file has popped up with many warning messages(not error) but warnings stating ***warning: local declaration of 'aView' hides instance variable.*** this warning appears everywhere aView and bView is used.

    Also I have warnings for removefromSuperview, ****'UIImageView' may not repsond to '-removefromSuperview'****

    So set up this way it compiles and runs just about the same as before. It isn't stopping the previous animation yet, but it will display one on top of the other.

    .m

    Code:
     
    
    - (IBAction)buttonPressed1:(id)sender
    {
    	int x=2;
    	if (x=2){
    		NSLog(@"%i", x);
    		
    	UIImageView* aView = [[UIImageView alloc] initWithFrame:self.view.frame];
    	
    	aView.animationImages = [NSArray arrayWithObjects:	
    		
    ***warning: local declaration of 'aView' hides instance variable.***
    							
    		[UIImage imageNamed:@"01.png"],
    				.....(many more pngs)......
    	
    	aView.animationDuration = 2.00;
    
    ***warning: local declaration of 'aView' hides instance variable.***
    	
    	[bView stopAnimating];
    [bView removefromSuperview];
    
    *****Warning: UIImageView' may not repsond to '-removefromSuperview'****
    	
            [aView startAnimating];
    	[self.view addSubview:aView];
    	//[aView release]; 
    	}
    }
    
    
    So what do you think? I really appreciate your help.
    Thank you.
     
  4. jjgraz thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #4
    btw:

    I most concerned with the warning:

    UIImageView' may not respond to 'removefromSuperview'


    pretty sure i understand all the other warnings.....(the name in local is same as name in object so compiler doesn't know which one to use.)


    But can't figure out the removefromSuperview.

    Thank you.
     
  5. jjgraz thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #5
    also fyi:

    all the code above is in my mainViewController.h & .m files.
    (not MainView)

    In MainView.h

    I only have:
    Code:
    #import <UIKit/UIKit.h>
    
    @interface MainView : UIView {
    
    }
    
    @end
    
    MainView.m

    Code:
    #import "MainView.h"
    
    @implementation MainView
    
    
    - (id)initWithFrame:(CGRect)frame {
    	if (self = [super initWithFrame:frame]) {
    		// Initialization code
    	}
    	return self;
    }
    
    
    - (void)drawRect:(CGRect)rect {
    	// Drawing code
    }
    
    
    - (void)dealloc {
    	[super dealloc];
    }
    
    
    @end
    
     
  6. jjgraz thread starter macrumors regular

    jjgraz

    Joined:
    Feb 13, 2009
    #6
    got it. Thank you for putting me on right track.
     
  7. idelovski macrumors regular

    Joined:
    Sep 11, 2008
    #7
    If you guys still have this warning, try removeFromSuperview, with the capital F. This is my most common mistake so I am trained now to look for capitalization problems.
     
  8. drf1229 macrumors regular

    drf1229

    Joined:
    Jun 22, 2009
    #8
    Sorry, try this instead:

    [bView setHidden:YES];
     

Share This Page