07-31-2009, 03:57 AM
#1 (permalink )
Registered Member
Join Date: May 2009
Posts: 28
programmatically add animated UIImageView?
hi
I am adding a UIImageView programmatically into the view and filling it with an array of UImage objects and asking it to animate. However, nothing happens. Not even the first image displays in the view. I have previously been able to add one in using interface builder however that would prove impractical later on in my project.
This is my code:
Code:
aExplo.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"RGB0001.png"],
[UIImage imageNamed:@"RGB0002.png"],
[UIImage imageNamed:@"RGB0003.png"],
[UIImage imageNamed:@"RGB0004.png"],
[UIImage imageNamed:@"RGB0005.png"],
[UIImage imageNamed:@"RGB0006.png"],
[UIImage imageNamed:@"RGB0007.png"],
[UIImage imageNamed:@"RGB0008.png"],
[UIImage imageNamed:@"RGB0009.png"],
[UIImage imageNamed:@"RGB0010.png"],
[UIImage imageNamed:@"RGB0011.png"],
[UIImage imageNamed:@"RGB0012.png"],
[UIImage imageNamed:@"RGB0013.png"],
[UIImage imageNamed:@"RGB0014.png"],
[UIImage imageNamed:@"RGB0015.png"]];
aExplo.animationRepeatCount = 1;
aExplo.animationDuration = 1;
aExplo.center = CGPointMake(100, 100);
[aExplo startAnimating];
[self.view addSubview:aExplo];
I set an @property (nonatomic, retain) and an @synthesize for the aExplo UIImageView. Is there anything im missing?
Thanks
Jetwilson
07-31-2009, 04:33 AM
#2 (permalink )
Registered Member
Join Date: Jul 2009
Location: Los Angeles
Posts: 352
Did you set the frame for the UIImageView to display in?
07-31-2009, 04:37 AM
#3 (permalink )
Registered Member
Join Date: May 2009
Posts: 28
Quote:
Originally Posted by
kelvinkao
Did you set the frame for the UIImageView to display in?
err... no.
I couldn't see anything in the documentation about setting a frame! What do I need to do to set a frame? do I need to call initWithFrame:?
EDIT: okay, i tried this code before I add images to the array to create a frame:
Code:
UIImage *image = [UIImage imageNamed:@"RGB0001.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
aExplo = [[UIImageView alloc] initWithFrame:frame];
One small problem - the program now crashes. Am I setting the frame correctly?
jetwilson
Last edited by jetwilson; 07-31-2009 at 04:49 AM .
Reason: added sample code
07-31-2009, 06:22 AM
#4 (permalink )
Registered Member
Join Date: Jul 2009
Posts: 11
the frame looks fine. Where does your app crash? What does the debugger say?
07-31-2009, 06:40 AM
#5 (permalink )
Registered Member
Join Date: May 2009
Posts: 28
Quote:
Originally Posted by
imapps
the frame looks fine. Where does your app crash? What does the debugger say?
It crashes the next line after the frame code - the one where I create an array of images for the animationImages:
Code:
aExplo.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"RGB0001.png"],
[UIImage imageNamed:@"RGB0002.png"],
[UIImage imageNamed:@"RGB0003.png"],
[UIImage imageNamed:@"RGB0004.png"],
[UIImage imageNamed:@"RGB0005.png"],
[UIImage imageNamed:@"RGB0006.png"],
[UIImage imageNamed:@"RGB0007.png"],
[UIImage imageNamed:@"RGB0008.png"],
[UIImage imageNamed:@"RGB0009.png"],
[UIImage imageNamed:@"RGB0010.png"],
[UIImage imageNamed:@"RGB0011.png"],
[UIImage imageNamed:@"RGB0012.png"],
[UIImage imageNamed:@"RGB0013.png"],
[UIImage imageNamed:@"RGB0014.png"],
[UIImage imageNamed:@"RGB0015.png"]];
The last thing it says in the debugger window is objc_msgsend if that helps.
thanks
jetwilson
07-31-2009, 06:51 AM
#6 (permalink )
Registered Member
Join Date: Jul 2009
Posts: 11
You're missing a nil after the last object in array. arrayWithObjects: requires a comma separated array items ending with nil like this:
Code:
aExplo.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"RGB0001.png"],
[UIImage imageNamed:@"RGB0002.png"],
[UIImage imageNamed:@"RGB0003.png"],
[UIImage imageNamed:@"RGB0004.png"],
[UIImage imageNamed:@"RGB0005.png"],
[UIImage imageNamed:@"RGB0006.png"],
[UIImage imageNamed:@"RGB0007.png"],
[UIImage imageNamed:@"RGB0008.png"],
[UIImage imageNamed:@"RGB0009.png"],
[UIImage imageNamed:@"RGB0010.png"],
[UIImage imageNamed:@"RGB0011.png"],
[UIImage imageNamed:@"RGB0012.png"],
[UIImage imageNamed:@"RGB0013.png"],
[UIImage imageNamed:@"RGB0014.png"],
[UIImage imageNamed:@"RGB0015.png"],
nil];
07-31-2009, 07:16 AM
#7 (permalink )
Registered Member
Join Date: May 2009
Posts: 28
Quote:
Originally Posted by
imapps
You're missing a nil after the last object in array. arrayWithObjects: requires a comma separated array items ending with nil like this:
Code:
aExplo.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"RGB0001.png"],
[UIImage imageNamed:@"RGB0002.png"],
[UIImage imageNamed:@"RGB0003.png"],
[UIImage imageNamed:@"RGB0004.png"],
[UIImage imageNamed:@"RGB0005.png"],
[UIImage imageNamed:@"RGB0006.png"],
[UIImage imageNamed:@"RGB0007.png"],
[UIImage imageNamed:@"RGB0008.png"],
[UIImage imageNamed:@"RGB0009.png"],
[UIImage imageNamed:@"RGB0010.png"],
[UIImage imageNamed:@"RGB0011.png"],
[UIImage imageNamed:@"RGB0012.png"],
[UIImage imageNamed:@"RGB0013.png"],
[UIImage imageNamed:@"RGB0014.png"],
[UIImage imageNamed:@"RGB0015.png"],
nil];
Thank you! It worked perfectly! I cant believe I missed something so simple!
Jetwilson
07-31-2009, 07:19 AM
#8 (permalink )
Registered Member
Join Date: Jul 2009
Posts: 11
np
Glad I could help.
07-31-2009, 08:46 AM
#9 (permalink )
Registered Member
Join Date: Feb 2009
Posts: 60
meh, I come too late!
Anyhow I just finished a tutorial on just that topic:
Link
07-31-2009, 01:53 PM
#10 (permalink )
Registered Member
Join Date: May 2009
Posts: 28
new problem!
The previous problem was me just starting at the basics, once I had that sorted I thought I would expand and make this code more reusable. But yet another block has reared its ugly head.
I thought, to make the animation code more reusable and efficient I would create a UIView object and then create instances of that class to use within the main view.
This is the code within the explosionView.m UIView object file:
Code:
- (id)init {
UIImage *image = [UIImage imageNamed:@"RGB0001.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
aExplosionView = [[UIImageView alloc] initWithFrame:frame];
[image release];
self.opaque = NO;
aExplosionView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"RGB0001.png"],
[UIImage imageNamed:@"RGB0002.png"],
[UIImage imageNamed:@"RGB0003.png"],
[UIImage imageNamed:@"RGB0004.png"],
[UIImage imageNamed:@"RGB0005.png"],
[UIImage imageNamed:@"RGB0006.png"],
[UIImage imageNamed:@"RGB0007.png"],
[UIImage imageNamed:@"RGB0008.png"],
[UIImage imageNamed:@"RGB0009.png"],
[UIImage imageNamed:@"RGB0010.png"],
[UIImage imageNamed:@"RGB0011.png"],
[UIImage imageNamed:@"RGB0012.png"],
[UIImage imageNamed:@"RGB0013.png"],
[UIImage imageNamed:@"RGB0014.png"],
[UIImage imageNamed:@"RGB0015.png"],
nil];
aExplosionView.animationRepeatCount = 1;
aExplosionView.animationDuration = 1;
aExplosionView.center = CGPointMake(0, 0);
[aExplosionView startAnimating];
return self;
}
pretty much exactly the same as before.
and in my mainViewController.m I do this to create an instance of the explosionView class in viewDidLoad:
Code:
UIImage *image = [UIImage imageNamed:@"RGB0001.png"];
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
explosionView *explo = [[explosionView alloc] initWithFrame:frame];
[image release];explo.center = CGPointMake(100, 100);
[self.view addSubview:explo];
I have done this before with UIImages for non-animation images - but when I tried with this animating UIImageView, a black square where my animation should be displayed is displayed. No crash, no nothing. Just a black square. Not terribly useful!
Anyone know what's happening?
Thanks
Jetwilson
Last edited by jetwilson; 08-01-2009 at 03:41 AM .
Reason: added initWithFrame: code and question
09-20-2010, 04:48 PM
#11 (permalink )
Registered Member
Join Date: Jul 2010
Location: PK
Posts: 27
Animated Image is not placed on the location i mentioned
Hi i have used the coding technique you mentioned above, My image has started animation. But it is not well placed at the location i mentioned
This is the code i am using
UIImage *myBalloonImage=[UIImage imageNamed:@"ABalloon.png"];
UIImageView *myBalloonAnimatedView = [[UIImageView alloc] initWithImage:myBalloonImage];
CGRect frameBalloon = CGRectMake(60, 20,myBalloonImage.size.height, myBalloonImage.size.width);
myBalloonAnimatedView = [[UIImageView alloc] initWithFrame:frameBalloon];
[myBalloonImage release];
NSArray *myBalloonImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"ABalloon.png"],
[UIImage imageNamed:@"CBalloon.png"],
[UIImage imageNamed:@"EBalloon.png"],
nil];
myBalloonAnimatedView.animationImages = myBalloonImages;
myBalloonAnimatedView.animationDuration = 2.0;
myBalloonAnimatedView.animationRepeatCount = 0;
[myBalloonAnimatedView startAnimating];
myBalloonAnimatedView.center=CGPointMake(160, 160);
[self.view addSubview:myBalloonAnimatedView];
[myBalloonAnimatedView release];
Now kindly tell me in hurry where i am doing mistake. According to me , There is some problem i am facing in making frames.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 326
11 members and 315 guests
chiataytuday , coolman , givensur , guusleijsten , ipodphone , jbro , mediaspree , mottdog , mtl_tech_guy , Punkjumper , vilisei
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,882
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl