开发者

Annimation in iphone inside for loop not working

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 setAnimationRepeat开发者_开发技巧Count: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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜