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

jjgraz

macrumors regular
Original poster
Feb 13, 2009
103
0
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.
 
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
 
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.
 
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.
 
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
 
But can't figure out the removefromSuperview.

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.
 
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.
Sorry, try this instead:

[bView setHidden:YES];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.