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...
Related
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!
Hi i have a uislider that changes the images in uiimageview.At first time it works great there is no delay . But when i click the button to publish (there is a modal view that opens) and close the modal view (this is the second time) the uiimageview delays that shown the images.i cant find why it gets slower:(
-(IBAction)sliderSlide:(UISlider *)aSlider {
float f=slider.value;
NSString *show=[NSString stringWithFormat:#"%.0f %%",f];
label2.text=show;
NSString *show1=[NSString stringWithFormat:#"%.0f",f];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:#"/Smiley_000%#.png", show1]];
float slid=slider.value;
if(slid>99)
{
[imageView1 setHidden:NO];
[imageView2 setHidden:YES];
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/Smiley_00099.png"];
imageView.image = [UIImage imageWithContentsOfFile:fullpath];
secondarray=[NSArray arrayWithObjects:
[UIImage imageNamed:#"Rays_00000.png"],
[UIImage imageNamed:#"Rays_00001.png"],
[UIImage imageNamed:#"Rays_00002.png"],
[UIImage imageNamed:#"Rays_00003.png"],
[UIImage imageNamed:#"Rays_00004.png"],
[UIImage imageNamed:#"Rays_00005.png"],
[UIImage imageNamed:#"Rays_00006.png"],
[UIImage imageNamed:#"Rays_00007.png"],
[UIImage imageNamed:#"Rays_00008.png"],
[UIImage imageNamed:#"Rays_00009.png"],
nil];
imageView1.animationImages = secondarray;
// How many seconds it should take to go through all images one time.
imageView1.animationDuration = 0.5;
// How many times to repeat the animation (0 for indefinitely).
imageView1.animationRepeatCount = 0;
[imageView1 startAnimating];
}
else if (slid<1)
{
[imageView2 setHidden:NO];
[imageView1 setHidden:YES];
NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:#"/Smiley_0000.png"];
imageView.image = [UIImage imageWithContentsOfFile:fullpath];
firstarray=[NSArray arrayWithObjects:
[UIImage imageNamed:#"Teardrop_00000.png"],
[UIImage imageNamed:#"Teardrop_00001.png"],
[UIImage imageNamed:#"Teardrop_00002.png"],
[UIImage imageNamed:#"Teardrop_00003.png"],
[UIImage imageNamed:#"Teardrop_00004.png"],
[UIImage imageNamed:#"Teardrop_00005.png"],
[UIImage imageNamed:#"Teardrop_00006.png"],
[UIImage imageNamed:#"Teardrop_00007.png"],
[UIImage imageNamed:#"Teardrop_00008.png"],
[UIImage imageNamed:#"Teardrop_00009.png"],
[UIImage imageNamed:#"Teardrop_00000.png"],
[UIImage imageNamed:#"Teardrop_00000.png"],
[UIImage imageNamed:#"Teardrop_00000.png"],
[UIImage imageNamed:#"Teardrop_00000.png"],
[UIImage imageNamed:#"Teardrop_00000.png"],
nil];
imageView2.animationImages = firstarray;
// How many seconds it should take to go through all images one time.
imageView2.animationDuration = 0.8;
// How many times to repeat the animation (0 for indefinitely).
imageView2.animationRepeatCount = 0;
[imageView2 startAnimating];
[self.view addSubview:imageView2];
}
else
{
[imageView1 setHidden:YES];
[imageView2 setHidden:YES];
[imageView1 stopAnimating];
[imageView2 stopAnimating];
}
[[NSUserDefaults standardUserDefaults] setFloat:slid forKey:#"slider"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
this is my code if u can see why performance decrease in the second time i slide the slider pls tell me
edit 1:
i cancel some animations and slider get back to its speed but now the pictures are sticking slider okey but pictures are slow then the first time.
Well, I would say that your first step here to making this method more responsive is to remove all object allocations and unnecessary logic from this delegate method. The slider delegate method can be called over 100 times a second (if it is setup as continuous) and you should really be using it to only update ivars and perform minimal logic. This is most likely the root cause of your delays. As for why it is happening on the second time around, perhaps you are duplicating slider setup on your viewWillAppear, so after the modal, the delegate method is called twice as much.
My alertview makes the application (slider and images) goes slower so i remove it and it work just fine.
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!
can we use a gif image as a background?
i want to set an gif image as a background, like some birds flying ....
can we use gif image to set as background.???
if yes than how?
thanks and regards
Yes you can - I assume you want an animated gif?
It's the same CSS rule as a static background.
body {
background: url(animated.gif);
}
Here, i got the solution.
UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"default1.jpg"],
[UIImage imageNamed:#"default2.jpg"],
[UIImage imageNamed:#"default3.jpg"],
[UIImage imageNamed:#"default4.jpg"],
[UIImage imageNamed:#"default5.jpg"],
[UIImage imageNamed:#"default6.jpg"],
[UIImage imageNamed:#"default7.jpg"],
[UIImage imageNamed:#"default8.jpg"],
[UIImage imageNamed:#"default9.jpg"],
[UIImage imageNamed:#"default10.jpg"],
[UIImage imageNamed:#"default11.jpg"],
[UIImage imageNamed:#"default12.jpg"],
[UIImage imageNamed:#"default13.jpg"],
[UIImage imageNamed:#"default14.jpg"],
[UIImage imageNamed:#"default15.jpg"], nil];
animatedImageView.animationDuration = 3.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[self.view addSubview: animatedImageView];
Is there a way to freeze the last frame of my animation in my iphone app? Here's my code so far.
popup.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"picture1.png"],
[UIImage imageNamed:#"picture2.png"],
[UIImage imageNamed:#"picture3.png"],
[UIImage imageNamed:#"picture4.png"],
[UIImage imageNamed:#"picture5.png"],
[UIImage imageNamed:#"picture6.png"],
[UIImage imageNamed:#"picture7.png"], nil];
popup.animationDuration = 1.750567;
popup.animationRepeatCount = 1;
[popup startAnimating];
[self.view addSubview:popup];
When animation stops UIImageView shows UIImage set in its image property. So you should just set it to the last frame of your animation:
popup.image = [UIImage imageNamed:#"picture7.png"];