views:

203

answers:

2

Hello friends,

I am currently developing an application in iphone, i want to animate some images on screen and for the same i have written this code

    for (int i = 1 ; i <= 5 ; i++)
{
imgview.image = [UIImage imageNamed: [NSString stringWithFormat:@"spleshScreen%d.png", 1]];
NSLog(@"i = %d" , i);
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:0.9];
[UIView setAnimationRepeatCount:5];
[UIView setAnimationBeginsFromCurrentState:YES];
imgview.image = [UIImage imageNamed: [NSString stringWithFormat:@"spleshScreen%d.png", i]];
i++;
imgview.frame = CGRectMake(imgview.frame.origin.x, (imgview.frame.origin.y + 180), imgview.frame.size.width, imgview.frame.size.height);
imgview.frame = CGRectMake(imgview.frame.origin.x, (imgview.frame.origin.y - 350), imgview.frame.size.width, imgview.frame.size.height);

[UIView commitAnimations];
}

if i remove the for loop code works fine, but with for loop it's not working neither it get any error

Please let me know what can be the problem

A: 

UIView Class Reference: The animations are run in a separate thread and begin when the application returns to the run loop.

So when you start your loop you create some threads and there is may be problems with that. Try to delete your for loop and use setAnimationDidStopSelector method (see the UIView Class Reference):

[UIView setAnimationDidStopSelector:@selector(annimationDidFinish:)];

When your annimationDidFinish is called you can invoke a method to start an "other loop". I haven't tested this solution, but I just give you another track to help you.

Yannick L.
basically i want to move 5 images from bottom to top one by one animating and for this UIImageView's startAnimating won't work for this particular problem any solution is there?
mihirpmehta
+1  A: 

You can use the animationImages property of a UIImageView to handle the animation for you.

Here is a code sample:


imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed: [NSString stringWithFormat:@"spleshScreen%d.png", 1]],
                                                      [UIImage imageNamed: [NSString stringWithFormat:@"spleshScreen%d.png", 2]],
                                                      [UIImage imageNamed: [NSString stringWithFormat:@"spleshScreen%d.png", 3]],
                                                      [UIImage imageNamed: [NSString stringWithFormat:@"spleshScreen%d.png", 4]],
                                                      [UIImage imageNamed: [NSString stringWithFormat:@"spleshScreen%d.png", 5]],
                                                    nil];       // Don't forget to nil terminate this array!


[imageView setAnimationDuration:2.0];  // Define the amount of time it takes to go through one cycle of all the images.
// The property animationRepeatCount default value is 0, which specifies to repeat the animation indefinitely.

[imageView startAnimating];

In the last 2 lines of your code, you were changing the y origin of the frame of your image (adding 180 then substracting 350 which doesn't really make sense).
As you didn't speak about it in your question, I leaved this out.

Guillaume
initially image is in the middle of the view so i am adding 180 to set image at the bottom and then to send that image at the top i am subtracting by - 350 basically i want to move 5 images from bottom to top one by one animating and for this UIImageView's startAnimating won't work for this particular problem any solution ?
mihirpmehta