view images from array's index - objective-c

i created an array that contain images, i animate them via animationImages. but now i want to view image from the array index(i want to create a for loop with count, and if my count is equal to 1 so it will view the image at index 1 from the array) and animate them. i need it becuase i want to view number from 1 to 100 and if my number is equal to 11 then i will show number 1 twich(to avoid from creating 100 image of numbers).
how can i do it?
here is my correct code:
//Numbers images
UIImage *numberZero = [UIImage imageNamed:#"numberZero.png"];
UIImage *numberOne = [UIImage imageNamed:#"numberOne.png"];
UIImage *numberTwo = [UIImage imageNamed:#"numberTwo.png"];
UIImage *numberThree = [UIImage imageNamed:#"numberThree.png"];
UIImage *numberFour = [UIImage imageNamed:#"numberFour.png"];
UIImage *numberFive = [UIImage imageNamed:#"numberFive.png"];
UIImage *numberSix = [UIImage imageNamed:#"numberSix.png"];
UIImage *numberSeven = [UIImage imageNamed:#"numberSeven.png"];
UIImage *numberEight = [UIImage imageNamed:#"numberEight.png"];
UIImage *numberNine = [UIImage imageNamed:#"numberNine.png"];
//add numbers uiimage to numbersArray
numbersArray = [NSArray arrayWithObjects:numberZero, numberOne, numberTwo, numberThree, numberFour, numberFive, numberSix, numberSeven, numberEight, numberNine, nil];
UIImageView *imgview1 = [[UIImageView alloc] initWithFrame:CGRectMake(40, 90, 240, 240)];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=1;
[imgview1 startAnimating];
[self.view addSubview:imgview1];
thanks!

I'm a little unsure of your question, but I gather you're trying to animate counting to 100(?) using your images as the digits. Thus, use one UIImageView for each digit, all with the same animations images, and animate the different places at relative durations:
float onesDurations = 2;
UIImageView *onesPlace = [[UIImageView alloc] init...];
onesPlace.animationImages = numbersArray;
onesPlace.animationDuration = onesDurations;
UIImageView *tensPlace = [[UIImageView alloc] init...];
tensPlace.animationImages = numbersArray;
tensPlace.animationDuration = 10*onesDurations; // ten times as slow as ones animation
UIImageView *hundredsPlace = [[UIImageView alloc] init...];
hundredsPlace.animationImages = numbersArray;
hundredsPlace.animationDuration = 100*onesDurations; // 100 times as slow animation
// add them all to the view
// start their animations

You could display text instead of images with a custom font. If you want to add a border or outline, then you will have to do abit more work. this question explains how you can add an outline to your text.
hope this helps

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.

How to rotate a image (globe) around Y axis?

I have an image of globe which I want to rotate continuously around Y-axis. I also want to simulate it as an 3D rotation. I found some code using CABasicLayer and CALayer, but they rotate an image around Z-axis.
Try this code :
CABasicAnimation *rotateAnimation = [CABasicAnimation animation];
rotateAnimation.keyPath = #"transform.rotation.z";
rotateAnimation.fromValue = [NSNumber numberWithFloat:DegreesToRadians(0)];
rotateAnimation.toValue = [NSNumber numberWithFloat:DegreesToRadians(360)];
rotateAnimation.duration = 10;
rotateAnimation.removedOnCompletion = NO;
// leaves presentation layer in final state; preventing snap-back to original state
rotateAnimation.fillMode = kCAFillModeForwards;
rotateAnimation.repeatCount = 99;
rotateAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// add the animation to the selection layer. This causes it to begin animating
[imageView.layer addAnimation:rotateAnimation forKey:#"rotateAnimation"];
I end up using this because I need different views of globes. All images are view of globe taken from different angle. Here there are just 5 images but in actual I'll use around 36 images, because images will have 10 as difference of angle. Thus, 360/10 = 36.
UIImage *image = [UIImage imageNamed:#"globe.png"];
UIImage *image1 = [UIImage imageNamed:#"globe1.jpeg"];
UIImage *image2 = [UIImage imageNamed:#"globe2.jpeg"];
UIImage *image3 = [UIImage imageNamed:#"globe3.jpeg"];
UIImage *image4 = [UIImage imageNamed:#"globe4.jpeg"];
NSArray *imageArray = [NSArray arrayWithObjects:image, image1, image2, image3, image4, nil];
// set array of images, you want to cycle, to "animationImages" property.
self.imageView.animationImages = imageArray;
//duration of the animation-cycle
self.imageView.animationDuration = 2.0;
// 0 for endless repeation
self.imageView.animationRepeatCount = 0;
//starts the animation
[self.imageView startAnimating];

images from an array

i create an nsmutableArray and i add for it an images. also i create an imageView that i want to show the images from the array, i tried to use animationImages but i nothing is happened, how can i fix it?
here is my code:
//Numbers images
UIImage *numberZero = [UIImage imageNamed:#"numberZero.png"];
UIImage *numberOne = [UIImage imageNamed:#"numberOne.png"];
UIImage *numberTwo = [UIImage imageNamed:#"numberTwo.png"];
UIImage *numberThree = [UIImage imageNamed:#"numberThree.png"];
UIImage *numberFour = [UIImage imageNamed:#"numberFour.png"];
UIImage *numberFive = [UIImage imageNamed:#"numberFive.png"];
UIImage *numberSix = [UIImage imageNamed:#"numberSix.png"];
UIImage *numberSeven = [UIImage imageNamed:#"numberSeven.png"];
UIImage *numberEight = [UIImage imageNamed:#"numberEight.png"];
UIImage *numberNine = [UIImage imageNamed:#"numberNine.png"];
//add numbers uiimage to numbersArray
numbersArray = [NSMutableArray arrayWithObjects:numberZero, numberOne, numberTwo, numberThree, numberFour, numberFive, numberSix, numberSeven, numberEight, numberNine, nil];
imgview1 = [[UIImageView alloc] init];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=0;
[imgview1 startAnimating];
[imgview1 stopAnimating];
thanks!
You need to actually add imgview1 to something so that it displays. If imgview1 was created in Interface Builder then there is no need to assign. Also you stop your animation immediately after starting it.
//If created in IB comment out the next line
imgview1 = [[UIImageView alloc] init];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=0;
//If not created in IB then add this to a view e.g:
[self.view addSubview:imgview1];
[imgview1 startAnimating];
//[imgview1 stopAnimating]; this is too soon to stop animating
I assume that by "nothing happened" you mean that you see the first image, but the sequence does not move beyond that.
This is probably because you stopAnimating too soon: it does not even get a chance to start!
i solve the problem, i guess the problem were because i didn't use init with frame to my imgview.
here is the final code:
UIImageView *imgview1 = [[UIImageView alloc] initWithFrame:CGRectMake(40, 90, 240, 240)];
imgview1.animationImages = numbersArray;
imgview1.animationDuration = 2;
imgview1.animationRepeatCount=1;
[imgview1 startAnimating];
[self.view addSubview:imgview1];
hope u understand...

How to display image one by one when taking array?

I am beginner of iphone, I have all images in array but how to display one by one when I execute the array in a loop..
NSArray *images = [[NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
nil] retain];
int i=0;
int width = 0;
int height = 0;
for ( NSString *imageName in images )
{
//NSTimer *timer=[[NSTimer alloc]init];
UIImage *image = [images objectAtIndex:i];
//UIImage *image = [UIImage imageNamed:imageName];
animalphoto = [[UIImageView alloc] initWithImage:image];
animalphoto.contentMode = UIViewContentModeScaleAspectFit;
animalphoto.clipsToBounds = YES;
animalphoto.animationDuration=5.0;
animalphoto.animationRepeatCount=0;
animalphoto.frame = CGRectMake( animalphoto.frame.size.width * i++, 0, animalphoto.frame.size.width, animalphoto.frame.size.height);
width = animalphoto.frame.size.width;
height = animalphoto.frame.size.height;
NSLog(#"print:%#",image);
[animalphoto release];
}
}
Give any suggestion and sample code which is apply in our code..
Instead of putting one image just put whole array of images to the imgview.animationImages = images without the for loop
For further reference visit this site.
you need to use UIImageView's object animationImages to set the UIImage array
set animationRepeatCount
set animationDuration
then call [imageView startAnimating]; to start the animation

CABasicAnimation with 5 images

how to change this code to make the animation like this:
image1 --> image2 ---> image3 --->image4 --->image 5 .. then return back to image1 and so on ...
the code:
UIImage *image1 = [UIImage imageNamed:#"1.jpg"];
UIImage *image2 = [UIImage imageNamed:#"2.jpg"];
UIImage *image3 = [UIImage imageNamed:#"3.jpg"];
UIImage *image4 = [UIImage imageNamed:#"4.jpg"];
UIImage *image5 = [UIImage imageNamed:#"5.jpg"];
NSArray *imageArray = [NSArray arrayWithObjects:image1.CGImage, image2.CGImage, image3.CGImage, nil];
//self.introImages.image = image1;
[self.view addSubview:self.introImages];
CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:#"contents"];
crossFade.autoreverses = YES;
crossFade.repeatCount = HUGE_VALF;
crossFade.duration = 1.0;
//self.introImages.layer.contents = image2.CGImage;
crossFade.fromValue = (id)image1.CGImage;
crossFade.toValue = (id)image5.CGImage;
[self.introImages.layer addAnimation:crossFade forKey:#"animateContents"];
Better a late answer then no one i think. :)
There are several mistakes in your code.
First of all if you want to use crossfade of images you should add the animation to the imageView containing the images and just switch between the UIImages.
Second: If you want to cycle through more images than two, you should either use a queue of animations with the help of animationDidFinish callback methods, or you should use the animationImages field of the UIImageView class to let the UIImageView do all the work.
Third: If you are changing the layer of the image from image1 to image5 how could you expect the compiler to know that it should put all the other pics in between?
Try this code:
UIImage *image1 = [UIImage imageNamed:#"1.jpg"];
UIImage *image2 = [UIImage imageNamed:#"2.jpg"];
UIImage *image3 = [UIImage imageNamed:#"3.jpg"];
UIImage *image4 = [UIImage imageNamed:#"4.jpg"];
UIImage *image5 = [UIImage imageNamed:#"5.jpg"];
NSArray *imageArray = [NSArray arrayWithObjects:image1.CGImage, image2.CGImage, image3.CGImage, nil];
//imageView to add the array of images you want to cycle through
iv_introImages.animationImages = imageArray;
//duration of the animation-cycle
iv_introImages.animationDuration = 1.0;
//how often you want the animation to repeat. 0 stands for endless repetition
iv_introImages.animationRepeatCount = 0;
//starts the animation
[iv_introImages startAnimating];
hope this helps.
Mav