I need to access an NSArray and get a value from it with:
-(IBAction) randClicked:(id)sender
{
int randNum;
//when user clicks the button
//create random number, 1 - 32
randNum = arc4random() %32;
As you see, I can display my random number here:
///using this code for testing random number when button is clicked
NSString *randValText = [[NSString alloc]initWithFormat:#"%d", randNum];
//Output random number to label
labelRandText.text = randValText;
//Call method seeImage with SEL methodSelector
SEL methodSelector = #selector(seeImage);
///set image opacity to 0
wisdomView.alpha = 0.0;
//Use NSTimer to call method
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:methodSelector userInfo:nil repeats:NO];
[randValText release];
}
But now, I need to compare the value of randNum and access an NSArray of images for it's position in the array so I can display an image in an UIImageView.
Having a difficult time trying to figure this one out. All help is greatly appreciated. Thank you for your time.
Here is the current code that I have implemented with help from Josh as of 11:13pm 7/6/2001. I think I almost have it...but something I'm doing is not working. I keep getting the warning "Method '-objectAtIndex not found (return type defaults to 'id')
//Call method seeImage with SEL methodSelector
SEL methodSelector = #selector(seeImage);
//Use NSTimer to call method
[NSTimer scheduledTimerWithTimeInterval:0 target:self selector:methodSelector userInfo:[NSNumber numberWithInt:randNum] repeats:NO];
[randValText release];
}
////This is the method to make the graphic choosen by randNum to display at the top of the screen
-(IBAction) seeImage: (NSTimer *)tim
{
CGContextRef imageContext = UIGraphicsGetCurrentContext();
int randNum = [(NSNumber *)[tim userInfo] intValue];
UIImage *imgToDisplay = [wisdomView objectAtindex:randNum];
wisdomView.image = imgToDisplay;
wisdomView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"0.png"],
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"11.png"],
[UIImage imageNamed:#"12.png"],
[UIImage imageNamed:#"13.png"],
[UIImage imageNamed:#"14.png"],
[UIImage imageNamed:#"15.png"],
[UIImage imageNamed:#"16.png"],
[UIImage imageNamed:#"17.png"],
[UIImage imageNamed:#"18.png"],
[UIImage imageNamed:#"19.png"],
[UIImage imageNamed:#"20.png"],
[UIImage imageNamed:#"21.png"],
[UIImage imageNamed:#"22.png"],
[UIImage imageNamed:#"23.png"],
[UIImage imageNamed:#"24.png"],
[UIImage imageNamed:#"25.png"],
[UIImage imageNamed:#"26.png"],
[UIImage imageNamed:#"27.png"],
[UIImage imageNamed:#"28.png"],
[UIImage imageNamed:#"29.png"],
[UIImage imageNamed:#"30.png"],
[UIImage imageNamed:#"31.png"],nil];
wisdomView.alpha = 0;
[UIView beginAnimations:nil context:imageContext];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:4];
[UIView setAnimationDelegate:self];
wisdomView.alpha = 1;
[UIView commitAnimations];
SEL methodSelector = #selector(hideImage);
[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:methodSelector userInfo:nil repeats:NO];
}
I appreciate the help. Thank you for your time.
Here is the changed code that still doesn't work....I'm not understanding something. 1:19PM 7/7/2011
-(IBAction) seeImage:(NSTimer *)tim
{
NSArray *wisdom;
wisdom = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"0.png"],
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"11.png"],
[UIImage imageNamed:#"12.png"],
[UIImage imageNamed:#"13.png"],
[UIImage imageNamed:#"14.png"],
[UIImage imageNamed:#"15.png"],
[UIImage imageNamed:#"16.png"],
[UIImage imageNamed:#"17.png"],
[UIImage imageNamed:#"18.png"],
[UIImage imageNamed:#"19.png"],
[UIImage imageNamed:#"20.png"],
[UIImage imageNamed:#"21.png"],
[UIImage imageNamed:#"22.png"],
[UIImage imageNamed:#"23.png"],
[UIImage imageNamed:#"24.png"],
[UIImage imageNamed:#"25.png"],
[UIImage imageNamed:#"26.png"],
[UIImage imageNamed:#"27.png"],
[UIImage imageNamed:#"28.png"],
[UIImage imageNamed:#"29.png"],
[UIImage imageNamed:#"30.png"],
[UIImage imageNamed:#"31.png"],nil];
int randNum = [(NSNumber *)[tim userInfo] intValue];
CGContextRef imageContext = UIGraphicsGetCurrentContext();
UIImage *imgToDisplay = [wisdom objectAtIndex:randNum];
wisdomView.image = imgToDisplay;
Still getting the Method '-objectAtIndex:' not found warning and now '' may not respond to '' 'NSArray' may not respond to '-objectAtIndex'. Arrrrgggg.
Thank you for your time.
Tuck the generated number into the timer's user info slot:
[NSTimer scheduledTimerWithTimeInterval:0
target:self
selector:#selector(seeImage:)
userInfo:[NSNumber numberWithInt:randNum]
repeats:NO];
and then get it back out in the timer's method. By the way, timer methods are supposed to have one parameter.* The timer passes itself to the method for just such a case as this, i.e., when you need to send some info along to be used when it fires.
- (void) seeImage: (NSTimer *)tim {
// Retrieve the NSNumber, using a cast because userInfo returns id
int randNum = [(NSNumber *)[tim userInfo] intValue];
NSImage * imgToDisplay = [arrayOfImgs objectAtIndex:randNum];
// and so on...
}
*Although they do work with any number of parameters, including 0.
Try:
[imageArray objectAtIndex:randNum]
EDIT: based on the additional info in the comments, I think #Josh Caswell's response better answers the question.
//create random number, 1 - 32
randNum = arc4random() %32;
that would give you 0 - 31. check your array bounds.
I fixed it! Thanks Josh for the assist!
-(IBAction) seeImage
{
int randNum;
//when user clicks the button
//create random number, 0 - 31
randNum = arc4random() %32;
NSString *randValText = [[NSString alloc]initWithFormat:#"%d", randNum];
//Output random number to label - testing code
//labelRandText.text = randValText;
///set image opacity to 0
wisdomView.alpha = 0.0;
wisdom = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"0.png"],
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
[UIImage imageNamed:#"5.png"],
[UIImage imageNamed:#"6.png"],
[UIImage imageNamed:#"7.png"],
[UIImage imageNamed:#"8.png"],
[UIImage imageNamed:#"9.png"],
[UIImage imageNamed:#"10.png"],
[UIImage imageNamed:#"11.png"],
[UIImage imageNamed:#"12.png"],
[UIImage imageNamed:#"13.png"],
[UIImage imageNamed:#"14.png"],
[UIImage imageNamed:#"15.png"],
[UIImage imageNamed:#"16.png"],
[UIImage imageNamed:#"17.png"],
[UIImage imageNamed:#"18.png"],
[UIImage imageNamed:#"19.png"],
[UIImage imageNamed:#"20.png"],
[UIImage imageNamed:#"21.png"],
[UIImage imageNamed:#"22.png"],
[UIImage imageNamed:#"23.png"],
[UIImage imageNamed:#"24.png"],
[UIImage imageNamed:#"25.png"],
[UIImage imageNamed:#"26.png"],
[UIImage imageNamed:#"27.png"],
[UIImage imageNamed:#"28.png"],
[UIImage imageNamed:#"29.png"],
[UIImage imageNamed:#"30.png"],
[UIImage imageNamed:#"31.png"],nil];
CGContextRef imageContext = UIGraphicsGetCurrentContext();
//pick image randNum index position
wisdomView.image = [wisdom objectAtIndex:randNum];
wisdomView.alpha = 0.0;
[UIView beginAnimations:nil context:imageContext];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:4];
[UIView setAnimationDelegate:self];
wisdomView.alpha = 1;
[UIView commitAnimations];
SEL methodSelector = #selector(hideImage);
[NSTimer scheduledTimerWithTimeInterval:4 target:self selector:methodSelector userInfo:nil repeats:NO];
[randValText release];
}
Thank you for your time!
Related
I have added a UIView, On that UIImageView using UIBuider,
Now I want to apply animation on that imageView, so I'v taken an array of images & adding that
array to imageView through this code
NSArray *animationArray=[[NSArray alloc]init];
animationArray=[NSArray arrayWithObjects:
[UIImage imageNamed:#"rabbit1.png"],
[UIImage imageNamed:#"rabbit2.png"],
[UIImage imageNamed:#"rabbit3.png"],
[UIImage imageNamed:#"rabbit4.png"],
[UIImage imageNamed:#"rabbit5.png"],
[UIImage imageNamed:#"rabbit6.png"],
[UIImage imageNamed:#"rabbit7.png"],
[UIImage imageNamed:#"rabbit8.png"],
[UIImage imageNamed:#"rabbit9.png"],
[UIImage imageNamed:#"rabbit10.png"],
[UIImage imageNamed:#"rabbit11.png"],
[UIImage imageNamed:#"rabbit12.png"],
[UIImage imageNamed:#"rabbit13.png"], nil];
tempImageView.animationImages=animationArray;
tempImageView.animationDuration=2;
tempImageView.animationRepeatCount=0;
[tempImageView startAnimating];
tempImageView is created using UIBuider
But it is not woking, isn't showing anything.
There seems to a bug in the story board auto layout..i found the same no result..
Try it via code this works..
mainImageView = [[UIImageView alloc]init];
NSArray *animationArray = [[NSArray alloc]init];
animationArray = [NSArray arrayWithObjects:[UIImage imageNamed:#"first.png"],[UIImage imageNamed:#"second.png"],[UIImage imageNamed:#"first.png"],[UIImage imageNamed:#"second.png"],[UIImage imageNamed:#"first.png"],[UIImage imageNamed:#"second.png"],[UIImage imageNamed:#"first.png"],[UIImage imageNamed:#"second.png"],[UIImage imageNamed:#"first.png"],[UIImage imageNamed:#"second.png"], nil]; //add your images here
[mainImageView setFrame:CGRectMake(50,50,100,100)];
mainImageView.animationImages = animationArray; //mainImageView is imageview
mainImageView.animationDuration = 2;
mainImageView.animationRepeatCount = 0;
[mainImageView startAnimating];
[self.view addSubview:mainImageView];
I am using this piece of code for an animation in one of my views:
animation.animationImages= [NSArray arrayWithObjects:
[UIImage imageNamed:#"Frame0.png"],
[UIImage imageNamed:#"Frame1.png"],
[UIImage imageNamed:#"Frame2.png"],
[UIImage imageNamed:#"Frame3.png"],
[UIImage imageNamed:#"Frame4.png"],
[UIImage imageNamed:#"Frame5.png"],
[UIImage imageNamed:#"Frame6.png"],
[UIImage imageNamed:#"Frame7.png"],
[UIImage imageNamed:#"Frame8.png"],
[UIImage imageNamed:#"Frame9.png"],
[UIImage imageNamed:#"Frame10.png"],
[UIImage imageNamed:#"Frame11.png"],
[UIImage imageNamed:#"Frame12.png"],
[UIImage imageNamed:#"Frame13.png"],
[UIImage imageNamed:#"Frame14.png"],
nil];
[animation setAnimationRepeatCount:1];
animation.animationDuration = 1;
[animation startAnimating];
Now I need to state when the animation ends 2 buttons appear, 'menu' and 'replay'
Do i use stopanimating or something else, please help and explain as I am a beginner, thanks for any help :)
Well your animation duration is 1 second so you do something like this:
animation.animationImages= [NSArray arrayWithObjects:
[UIImage imageNamed:#"Frame0.png"],
[UIImage imageNamed:#"Frame1.png"],
[UIImage imageNamed:#"Frame2.png"],
[UIImage imageNamed:#"Frame3.png"],
[UIImage imageNamed:#"Frame4.png"],
[UIImage imageNamed:#"Frame5.png"],
[UIImage imageNamed:#"Frame6.png"],
[UIImage imageNamed:#"Frame7.png"],
[UIImage imageNamed:#"Frame8.png"],
[UIImage imageNamed:#"Frame9.png"],
[UIImage imageNamed:#"Frame10.png"],
[UIImage imageNamed:#"Frame11.png"],
[UIImage imageNamed:#"Frame12.png"],
[UIImage imageNamed:#"Frame13.png"],
[UIImage imageNamed:#"Frame14.png"],
nil];
[animation setAnimationRepeatCount:1];
animation.animationDuration = 1;
[animation startAnimating];
[self performSelector:#selector(didFinishAnimating) withObject:nil afterDelay:1.0];
-(void) didFinishAnimating {
//animation ended add some buttons
}
You can just use an NSTimer to call another method once the animation finishes, buy using the same time interval for both the NSTimer and the animationDuration property.
animation.animationImages= [NSArray arrayWithObjects:
[UIImage imageNamed:#"Frame0.png"],
[UIImage imageNamed:#"Frame1.png"],
[UIImage imageNamed:#"Frame2.png"],
[UIImage imageNamed:#"Frame3.png"],
[UIImage imageNamed:#"Frame4.png"],
[UIImage imageNamed:#"Frame5.png"],
[UIImage imageNamed:#"Frame6.png"],
[UIImage imageNamed:#"Frame7.png"],
[UIImage imageNamed:#"Frame8.png"],
[UIImage imageNamed:#"Frame9.png"],
[UIImage imageNamed:#"Frame10.png"],
[UIImage imageNamed:#"Frame11.png"],
[UIImage imageNamed:#"Frame12.png"],
[UIImage imageNamed:#"Frame13.png"],
[UIImage imageNamed:#"Frame14.png"],
nil];
[animation setAnimationRepeatCount:1];
animation.animationDuration = 1;
[animation startAnimating];
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:#selector(showOtherButtonsAfterAnimation:)
userInfo:nil
repeats:NO];
The NSTimer will call the method you set in selector(showOtherButtonsAfterAnimation:) of the target object(self) after the selected interval(1 second). The showOtherButtonsAfterAnimation: method needs to have an NSTimer argument even if you never use it in the method.
-(void)showOtherButtonsAfterAnimation:(NSTimer*)theTimer { .... whatever ..... }
I'm trying to freeze the last frame of an animation. The following code used to work, however after an update to iOS it no longer works. How can I write this code so that the last image stays on the screen?
animation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"watermelondrop2.png"],
[UIImage imageNamed:#"watermelondrop4.png"],
[UIImage imageNamed:#"watermelondrop6.png"],
[UIImage imageNamed:#"watermelondrop8.png"],
[UIImage imageNamed:#"watermelondrop10.png"],
[UIImage imageNamed:#"watermelondrop12.png"],
[UIImage imageNamed:#"watermelondrop14.png"],
[UIImage imageNamed:#"watermelondrop15.png"], nil];
animation.animationDuration = .8;
animation.animationRepeatCount = 1;
[animation startAnimating];
[self playwatermelondropsound];
[self.view addSubview:animation];
animation.image = [UIImage imageNamed:#"watermelondrop16.png"];
I spend 40 my minutes to resolve your problem, and it was just so simple:
There are two way you can achieve what you want:
Set the image you want on your screen as default image.
or just do this,
//make your last line to first line.
animation.image = [UIImage imageNamed:#"watermelondrop16.png"];
animation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"watermelondrop2.png"],
[UIImage imageNamed:#"watermelondrop4.png"],
[UIImage imageNamed:#"watermelondrop6.png"],
[UIImage imageNamed:#"watermelondrop8.png"],
[UIImage imageNamed:#"watermelondrop10.png"],
[UIImage imageNamed:#"watermelondrop12.png"],
[UIImage imageNamed:#"watermelondrop14.png"],
[UIImage imageNamed:#"watermelondrop15.png"], nil];
animation.animationDuration = .8;
animation.animationRepeatCount = 1;
[animation startAnimating];
[self playwatermelondropsound];
[self.view addSubview:animation];
And guess what it'll start working, as I have tested it. :)
[self cleanStance];
NSArray *imageArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"jump1.png"],
[UIImage imageNamed:#"jump2.png"],
nil];
self.player.animationImages = imageArray;
self.player.animationDuration = 0.3;
//self.player.contentMode = UIViewContentModeBottomLeft;
[self.view addSubview:self.player];
[self.player startAnimating];
- (void)cleanStance {
[self.player setImage:nil];
self.player.animationImages = nil;
}
This is the example to show the image jumping i thick this may help u
Your code looks fine to me, except when I do it, I usually set the .image property of the UIImageView BEFORE I tell the UIImageView to start animating. Also, add [UIImage imageNamed:#"watermelondrop16.png"] to the end of your .animationImages array. Maybe that will fix your problem. Hope that Helps!
here is my code :
-(void) createNewImage {
image.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"oeufgrisé.png"],
[UIImage imageNamed:#"abouffer_03.png"],
[UIImage imageNamed:#"boutonplay_03.png"],
[UIImage imageNamed:#"boutonpause_03.png"],
[UIImage imageNamed:#"abouffer_05.png"],
[UIImage imageNamed:#"mangeurcentremieu3_03.png"],nil];
[image setAnimationRepeatCount:10];
image.animationDuration =1.5;
[image startAnimating];
imageView = [[UIImageView alloc] initWithImage:image];
}
This code doesn't work, I don't know why. I have always this warning on line "imageView = [[UIImageView alloc] initWithImage:image];" :Incompatible ObjectiveC type struct UIImageView*' expected struct UIImage*' when passing argument 1 of initWithImage from distinct Objective C type
You can set animation images to UIImageView , and not to UIImage.
-(void) createNewImage {
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,460)];
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"oeufgrisé.png"],
[UIImage imageNamed:#"abouffer_03.png"],
[UIImage imageNamed:#"boutonplay_03.png"],
[UIImage imageNamed:#"boutonpause_03.png"],
[UIImage imageNamed:#"abouffer_05.png"],
[UIImage imageNamed:#"mangeurcentremieu3_03.png"],nil];
[imageView setAnimationRepeatCount:10];
imageView.animationDuration =1.5;
[imageView startAnimating];
[NSTimer scheduledTimerWithTimeInterval:15.0 target:self selector:#selector(setLastImage:) userInfo:nil repeats:NO];
}
-(void)setLastImage:(id)obj
{
[imageView performSelectorOnMainThread:#selector(setImage:) withObject:[UIImage imageNamed:#"mangeurcentremieu3_03.png"] waitUntilDone:YES];
}
If you are doing:
image.animationImages = [NSArray arrayWithObjects:
then image is an UIImageView (review the code where you allocate it). That is the reason why you get the error, since initWithImage expects an object of type UIImage:
- (id)initWithImage:(UIImage *)image
Either you did not mean image to be an image view, or possibly you don't need to allocate a second UIImageView in your method and can just use image.
Make sure that image is a UIImageView and not a UIImage. You can only create animationImages for UIImageViews.
The code I wrote for 3.2 performs differently in OS4. Still no errors, just new bugs.
So I'm initializing the imageView inside an IBAction called "randomize" that produces and random number that goes to one of 86 cases producing a resulting animation and image, the only difference in code between 0 and 86 is that final imageView.image (I just didn't want to past 86 copies of the same code in for you to have to wade through ). No matter what the case, the animation imageView.animationImages runs. The bug is that the first time I run the action, imageView.animationImages runs showing my animation, then instead of completing the code and going to the imageView.image = [UIImage imageNamed:#"image36.png"], it now just shows whatever I have set as the actual imageView background in Interface Builder. And Sometimes it runs the animation for one case and the imageview.image for another. Any ideas?
- (IBAction)randomize {
int Number = arc4random() % 86;
switch (Number) {
case 0:
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],
[UIImage imageNamed:#"image4.png"],
[UIImage imageNamed:#"image5.png"],
[UIImage imageNamed:#"image6.png"],
[UIImage imageNamed:#"image7.png"],
[UIImage imageNamed:#"image8.png"],
[UIImage imageNamed:#"image9.png"],
[UIImage imageNamed:#"image10.png"],
[UIImage imageNamed:#"image0.png"],nil];
imageView.animationDuration = 0.50;
[imageView setAnimationRepeatCount: 1];
[imageView startAnimating];
imageView.image = [UIImage imageNamed:#"image36.png"];
break;
case 1:
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"image1.png"],
[UIImage imageNamed:#"image2.png"],
[UIImage imageNamed:#"image3.png"],
[UIImage imageNamed:#"image4.png"],
[UIImage imageNamed:#"image5.png"],
[UIImage imageNamed:#"image6.png"],
[UIImage imageNamed:#"image7.png"],
[UIImage imageNamed:#"image8.png"],
[UIImage imageNamed:#"image9.png"],
[UIImage imageNamed:#"image10.png"],
[UIImage imageNamed:#"image0.png"],nil];
imageView.animationDuration = 0.50;
[imageView setAnimationRepeatCount: 1];
[imageView startAnimating];
imageView.image = [UIImage imageNamed:#"image37.png"];
break;
If I had to guess, you're missing break statments at random intervals in your 86 cases. Please consider refactoring to a method...