Cocoa Touch - Play an Animation Once - cocoa-touch

I have this code but it plays an animation on a loop and I only want it to play once.
Any help?
-(void)gameOver{ //blow up ship
//animate explostion
UIImage *firstBoom = [UIImage imageNamed:#"Explo_0.png"];
UIImageView *bigBoom = [[UIImageView alloc] initWithImage:firstBoom];
bigBoom.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"Explo_1.png"],
[UIImage imageNamed:#"Explo_1.png"],
[UIImage imageNamed:#"Explo_1.png"],
[UIImage imageNamed:#"Explo_2.png"],
[UIImage imageNamed:#"Explo_2.png"],
[UIImage imageNamed:#"Explo_2.png"],
[UIImage imageNamed:#"Explo_3.png"],
[UIImage imageNamed:#"Explo_3.png"],
[UIImage imageNamed:#"Explo_3.png"],
[UIImage imageNamed:#"Explo_4.png"],
[UIImage imageNamed:#"Explo_4.png"],
[UIImage imageNamed:#"Explo_4.png"],
[UIImage imageNamed:#"Explo_4.png"],
[UIImage imageNamed:#"Explo_4.png"],
nil];
bigBoom.center = CGPointMake(xCoordinate, yCoordinate); //so it explodes ontop of the ship
[bigBoom startAnimating];
[self.view addSubview:bigBoom];
[shipImageView setHidden:YES];
}

Add the line:
bigBoom.animationRepeatCount = 1;

Related

How to use animated Navigation bar button in Objective c

I want to add this Image on my Navigation Bar button, How can I use this image on my navigation Right Bar button or Left bar button?
Please check below code those I am using to View same like work code on Navigation bar button:-
- (void)viewDidLoad
{
[super viewDidLoad];
// Animation
UIImageView*animationView = [[UIImageView alloc] initWithFrame:self.view.frame];
animationView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.gif"],
[UIImage imageNamed:#"2.gif"],
[UIImage imageNamed:#"3.gif"],
[UIImage imageNamed:#"4.gif"],
[UIImage imageNamed:#"5.gif"],
[UIImage imageNamed:#"6.gif"],nil];
animationView.animationDuration = 1.25;
animationView.animationRepeatCount = 0;
[animationView startAnimating];
[self.view addSubview:animationView];
}
Let me know How
to use this code on navigation bar button.
Thank You!
I have tried your code, with a little trick of mine and it work like magic.
NSArray *imageArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"tmp-0"],
[UIImage imageNamed:#"tmp-1"],
[UIImage imageNamed:#"tmp-2"],
[UIImage imageNamed:#"tmp-3"],
[UIImage imageNamed:#"tmp-4"],
[UIImage imageNamed:#"tmp-5"],
[UIImage imageNamed:#"tmp-6"],
[UIImage imageNamed:#"tmp-7"],nil];
UIButton *barButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barButton setImage:[UIImage imageNamed:#"tmp-0"] forState:UIControlStateNormal]; // mine trick
[barButton.imageView setAnimationImages:imageArray];
[barButton.imageView setAnimationDuration:1.0f];
[barButton.imageView startAnimating];
[barButton sizeToFit];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:barButton];
You need to give a image to barButton at first, so your button can make its frame

Fading background images automaticly in xcode

Im trying to fade several background images in xcode. They are animated but not fading, see code below:
animationgirl.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"girl1.png"],
[UIImage imageNamed:#"girl2.png"],
[UIImage imageNamed:#"girl3.png"],
[UIImage imageNamed:#"girl4.png"],
[UIImage imageNamed:#"girl5.png"],
[UIImage imageNamed:#"girl6.png"],
[UIImage imageNamed:#"girl7.png"],
[UIImage imageNamed:#"girl8.png"],
[UIImage imageNamed:#"girl9.png"],
[UIImage imageNamed:#"girl10.png"],
[UIImage imageNamed:#"girl11.png"],
[UIImage imageNamed:#"girl12.png"],
[UIImage imageNamed:#"girl13.png"],
[UIImage imageNamed:#"girl14.png"],nil];
[animationgirl setAnimationRepeatCount:0];
animationgirl.animationDuration = 1.8;
[animationgirl startAnimating];
Setting animationImages does not support an additional fade-in/fade-out effect when transitioning between images.
What you can do is creating a CAKeyframeAnimation and provide it with the array of images you are trying to animate, e.g.:
NSArray* contents = <NSArray of UIImages>;
CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:#"contents"];
[animation setCalculationMode:kCAAnimationLinear];
[animation setDuration:contentsAnimationDuration];
[animation setRepeatCount:HUGE_VALF];
[animation setValues:contents];
[self.layer addAnimation:animation forKey:#"contents"];
In order to use Core Animation, you have to link the Quartz framework and include the relevant header file.
You could also you a category on CALayer I wrote for this purpose.

UIButton GIF Background Image

How can I use a GIF image as UIButton Image Background?
UIImage *img1 = [UIImage imageNamed:#"btn_img1"];
UIImage *img2 = [UIImage imageNamed:#"btn_img2"];
UIImage *img3 = [UIImage imageNamed:#"btn_img3"];
// GIF ImageView
_btnGIF.imageView.animationImages = [[NSArray alloc] initWithObjects:img1, img2, img3, nil];
_btnGIF.imageView.animationDuration = 0;
[_btnGIF.imageView startAnimating];
//[_btnGIF setBackgroundImage:GIF forState:UIControlStateNormal];
You need to start your button with an image, so it can create it properly.
For example:
UIImage *img1 = [UIImage imageNamed:#"btn_img1"];
UIImage *img2 = [UIImage imageNamed:#"btn_img2"];
UIImage *img3 = [UIImage imageNamed:#"btn_img3"];
[_btnGIF setImage:img1 forState:UIControlStateNormal];
// GIF ImageView
_btnGIF.imageView.animationImages = [[NSArray alloc] initWithObjects:img1, img2, img3, nil];
_btnGIF.imageView.animationDuration = 0;
[_btnGIF.imageView startAnimating];

layer of animation images in iphone

How I can animate two sets of images one on the other on xcode? I mean that one set is background or landscape and the other set is a figure in the background
Here's a basic approach you might try:
UIImageView* backdropScene = [[UIImageView alloc] initWithFrame:self.view.frame];
backdropScene.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"rainy_background01.png"],
[UIImage imageNamed:#"rainy_background02.png"],
[UIImage imageNamed:#"rainy_background03.png"],
[UIImage imageNamed:#"rainy_background04.png"], nil];
backdropScene.animationDuration = .2f; // image change rate, .2 seconds
backdropScene.animationRepeatCount = 0; // repeat infinitely
[self.view addSubview:backdropScene];
[backdropScene startAnimation];
UIImageView* foregroundAnimation = [[UIImageView alloc] initWithFrame:self.view.frame];
foregroundAnimation.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"man_walk01.png"],
[UIImage imageNamed:#"man_walk02.png"],
[UIImage imageNamed:#"man_walk03.png"],
[UIImage imageNamed:#"man_walk04.png"], nil];
foregroundAnimation.animationDuration = .5f; // image change rate, .2 seconds
foregroundAnimation.animationRepeatCount = 0; // repeat infinitely
[self.view addSubview:foregroundAnimation];
[foregroundAnimation startAnimation];

Using an animated UIImageView as an iPhone camera overlay

Like the title says, in the iPhone SDK, I want to create an animated UIImageView and use it as a camera overlay. However, nothing appears. I've been using the same setup to display a static image as an overlay, but when trying to use the following code, no overlay appears:
imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"cameraScreenOverlay1.png"],
[UIImage imageNamed:#"cameraScreenOverlay2.png"],
[UIImage imageNamed:#"cameraScreenOverlay3.png"],
[UIImage imageNamed:#"cameraScreenOverlay4.png"],
[UIImage imageNamed:#"cameraScreenOverlay4.png"],
[UIImage imageNamed:#"cameraScreenOverlay4.png"],
[UIImage imageNamed:#"cameraScreenOverlay3.png"],
[UIImage imageNamed:#"cameraScreenOverlay2.png"],
[UIImage imageNamed:#"cameraScreenOverlay1.png"],
nil];
imageView.animationDuration = 1.0;
imageView.animationRepeatCount = 0;
[imageView startAnimating];
I know the above code works when the imageView is not used as an overlay. Any thoughts? Is this just a limitation of the current SDK?
I am trying to do the same thing. It will work when its just an overlay image, but once I try to animate with an array of images, nothing shows.
I was loading the pngs all wrong with imageWithContentsOfFile. It wont load the image it its just the image file. It needs the actual path.
Try this something like this:
NSArray *animationImages = [NSArray arrayWithObjects:
[UIImage imageWithContentsOfFile:[[ NSBundle mainBundle] pathForResource:#"back1" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[ NSBundle mainBundle] pathForResource:#"back2" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[ NSBundle mainBundle] pathForResource:#"back3" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[ NSBundle mainBundle] pathForResource:#"back4" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[ NSBundle mainBundle] pathForResource:#"back5" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[ NSBundle mainBundle] pathForResource:#"back6" ofType:#"png"]],
[UIImage imageWithContentsOfFile:[[ NSBundle mainBundle] pathForResource:#"back7" ofType:#"png"]],nil];
imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
imageview.animationImages = [animationImages retain] ;
imageview.animationRepeatCount = 0;
imageview.animationDuration= 1;
[overlay.view addSubview:imageview];
[moviePlayerWindow addSubview:overlay.view];