I am creating an app and I have bumped into a problem.
My app has a play button, once the user presses it a animation plays and finishes.
I have created the above in code which I will display below, but I need some help.
When the player press's play, it should display a label saying shake to start, and when the user shake's the iPhone the label will disappear and the animation will start. I am not able to put the animation code inside the IBAction code. Please help :)
-(IBAction)startanimation {
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];
button.hidden = 1;
animation.hidden = 0;
Menu.hidden = 0;
replay.hidden = 0;
}
I am a beginner at coding so please explain any answers :)
Thanks
In your IBAction set a BOOL named something like "readyToShake" to TRUE. Then in your Shake listener check for if "readyToShake" is TRUE, if it is, start the animation. If it is FALSE, ignore the shake gesture.
Related
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!
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];
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...
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"];