Fading background images automaticly in xcode - objective-c

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.

Related

Animating images Objective-C

I'm using an animation block to iterate through an array of .png images to make it look like a character is walking across the screen. However, the quality of the images blurs when I scale the image down. Currently I am testing the animation with 19 images all in 371 x 379 resolution. However, I need them to appear smaller on the screen so I scaled them to 64 x 65 on my app. When I do this, the lines distort a little bit and when I pause the game you can really tell that the image quality isn't where it should be. Below is the code I'm using to iterate through the images. Any help would be greatly appreciated.
[UIImageView animaiteWithDuration: 3.0
delay: 0
options: UIViewAnimationOptionBeginFromCurrentState
animations: ^{
CABasicAnimation *mover = [CABasicAnimation animationWithKeyPath:#"position"];
// Get the current presentationLayer of the object
CALayer *currentLayer = self.layer.presentationLayer;
// Get the current point of the presentationLayer
CGPoint currentPoint;
currentPoint.x = currentLayer.position.x;
currentPoint.y = currentLayer.position.y;
// Set the new point for the object to move to
CGPoint newPoint;
newPoint.x = currentLayer.position.x - 50;
newPoint.x = currentLayer.position.y;
// Set the from and to values of the animation
[mover setFromValue: [NSValue valueWithCGPoint:currentPoint]];
[mover setToValue: [NSValue valueWithCGPoint:newPoint]];
[mover setDuration:3.0]; // Set the duration of the mover animation
// Create the nine-teen UIImages for the array
UIImage *image1 = [UIImage imageNamed:#"img1"];
UIImage *image2 = [UIImage imageNamed:#"img2"];
UIImage *image3 = [UIImage imageNamed:#"img3"];
UIImage *image4 = [UIImage imageNamed:#"img4"];
UIImage *image5 = [UIImage imageNamed:#"img5"];
UIImage *image6 = [UIImage imageNamed:#"img6"];
UIImage *image7 = [UIImage imageNamed:#"img7"];
UIImage *image8 = [UIImage imageNamed:#"img8"];
UIImage *image9 = [UIImage imageNamed:#"img9"];
UIImage *image10 = [UIImage imageNamed:#"img10"];
UIImage *image11 = [UIImage imageNamed:#"img11"];
UIImage *image12 = [UIImage imageNamed:#"img12"];
UIImage *image13 = [UIImage imageNamed:#"img13"];
UIImage *image14 = [UIImage imageNamed:#"img14"];
UIImage *image15 = [UIImage imageNamed:#"img15"];
UIImage *image16 = [UIImage imageNamed:#"img16"];
UIImage *image17 = [UIImage imageNamed:#"img17"];
UIImage *image18 = [UIImage imageNamed:#"img18"];
UIImage *image19 = [UIImage imageNamed:#"img19"];
// Populate the animation array
NSArray *walkingAnimation = [NSArray arrayWithObjects: img1, img2, img3, img4, img5, img6, img7, img8, img9, img10, img11, img12, img13, img14, img15, img16, img17, img18, img19, nil];
[self setAnimationImages: walkingAnimation]; // Set the animation images to the array
[self setAnimationRepeatCount:3]; // Repeat the animation every second
[self setAnimationDuration:1.0];
[self startAnimating]; // Start the animation
[[self layer] addAnimation:mover forKey:#"position"]; // Add the mover animation to the layer
[UIImageView commitAnimations];
[[self layer] setPosition:newPoint]; // Set the position to avoid bounce back
} completion:nil];
So if anyone can tell me if there is something extra I need to be doing to keep the images from distorting, or maybe it's the images themselves, I could really use some tips while I read everything in Apples API and check every forum on the internet.
Your issue should be nothing to do with code. You are resizing the images to a different aspect ratio. You should be very careful with image resizing as the image will naturally blur.
Your best option is to create accurate (sharp) images, purpose made for the size at which you will use them.

Xcode the image is way too large need to be the size of the png image used not massive

UIImageView *image = [[UIImageView alloc] initWithFrame:self.view.bounds];
image.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"BluePin.png"],
[UIImage imageNamed:#"GreenPin.png"],
[UIImage imageNamed:#"RedPin.png"],
[UIImage imageNamed:#"YellowPin.png"],
nil];
image.animationRepeatCount = 0;
image.animationDuration = 1.0;
[image startAnimating];
[image setFrame: CGRectMake(point.x-(image.bounds.size.width/2), point.y- (image.bounds.size.width/2), image.bounds.size.width, image.bounds.size.height)];
[self.view addSubview: image];
[UIView animateWithDuration:2.0 delay:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
[image setAlpha:0.0];
} completion:^(BOOL finished) {
[image removeFromSuperview];
}];
the images are coming out way too big they need to be their original size
basically creates the images animation where the user touches and it fades away after x seconds
You want the image view to be the same dimensions as the image it contains, right?
UIImage *image = [UIImage imageName:#"BluePin.png"];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,image.size.width,image.size.height];
Note that I'm using a different variable naming scheme than yours to distinguish between an image and an image view.

How to perform prolonged animations

I'm wondering how to perform prolonged animations in Objective-C. An example of what i'm looking to implement would be the following:
http://vimeo.com/38514156
The way I have done this before is using the following code:
NSArray *imgArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"car1.png"],
[UIImage imageNamed:#"car2.png"],
[UIImage imageNamed:#"car3.png"],
[UIImage imageNamed:#"car4.png"],
[UIImage imageNamed:#"car5.png"],
[UIImage imageNamed:#"car6.png"],
[UIImage imageNamed:#"car7.png"],
[UIImage imageNamed:#"car8.png"],
[UIImage imageNamed:#"car9.png"],
[UIImage imageNamed:#"car10.png"],
[UIImage imageNamed:#"car11.png"],
[UIImage imageNamed:#"car12.png"],
nil];
imgCar.animationImages = imgArray;
imgCar.animationDuration = 0.8;
imgCar.animationRepeatCount = 0; //0 means infinite
[imgCar startAnimating];
I'm wondering if there a better way to do something like the video i posted above? If the animation is going on for about 45 seconds I'm guessing i would need a few hundred images - which seems like it probably won't be just intensive, but also be quite a big application.

Cocoa Touch - Play an Animation Once

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;

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];