Object-c如何在不同的持续时间内依次为不同的图像集制作动画(iphone)

时间:2011-11-03 12:49:07

标签: iphone animation uiimageview uiimage core-animation

我需要一个接一个地动画10组图像(10个图像阵列) 每组将在不同的持续时间内动画。 我尝试用UIImageView这样做:

[myImageView1 setAnimationImages:imagesSet1];
[myImageView1 setAnimationRepeatCount:0];
[myImageView1 setAnimationDuration:2];
[myImageView1 startAnimation];


[myImageView1 setAnimationImages:imagesSet2];
[myImageView1 setAnimationRepeatCount:0];
[myImageView1 setAnimationDuration:5];
[myImageView1 startAnimation];

但这不起作用,它会显示最后的图像。

4 个答案:

答案 0 :(得分:3)

试试这段代码:

 NSArray *array1=[[NSArray alloc] initWithObjects:[UIImage imageNamed:@"ballImg1.png"],[UIImage imageNamed:@"ballImg3.png"],[UIImage imageNamed:@"ballImg4.png"],[UIImage imageNamed:@"ballImg5.png"],nil];

 NSArray *array2=[[NSArray alloc] initWithObjects:[UIImage imageNamed:@"ballAniImg1.png"],[UIImage imageNamed:@"ballAniImg3.png"],[UIImage imageNamed:@"ballAniImg4.png"],[UIImage imageNamed:@"ballAniImg5.png"],nil];

 NSArray *array3=[[NSArray alloc] initWithObjects:[UIImage imageNamed:@"ballRotImg1.png"],[UIImage imageNamed:@"ballRotImg3.png"],[UIImage imageNamed:@"ballRotImg4.png"],[UIImage imageNamed:@"ballRotImg5.png"],nil];

array4=[[NSArray alloc] initWithObjects:array1,array2,array3,nil];

 i1=0;   
 [self performSelector:@selector(animation1:) withObject:[NSNumber numberWithFloat:2.0] afterDelay:0];
 i=0;



 -(void)animation1:(NSNumber*)k
{
if(i1<[array4 count])
{
int m1=[k floatValue];
NSArray *arr1= [array4 objectAtIndex:i1];
myImageView1.animationImages=arr1;
myImageView1.animationDuration=m1;
myImageView1.animationRepeatCount=1;
[myImageView1 startAnimating];
i1++;
    [self performSelector:@selector(animation1:) withObject:[NSNumber numberWithFloat:2.0] afterDelay:3.0];

}
}

答案 1 :(得分:2)

尝试此代码,更容易,所以使用它享受

animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];

animatedImageView.animationImages = [NSArray arrayWithObjects:
                                     [UIImage imageNamed:@"1.png"],
                                     [UIImage imageNamed:@"2.png"],
                                     [UIImage imageNamed:@"3.png"],
                                     [UIImage imageNamed:@"4.png"],
                                     [UIImage imageNamed:@"5.png"],
                                     [UIImage imageNamed:@"6.png"],nil];
animatedImageView.animationDuration = 20.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];

答案 2 :(得分:1)

尝试使用块

-(void)showImage1 {
    [UIView animateWithDuration:2.0
                          delay:0.0 
                        options:UIViewAnimationOptionTransitionFlipFromLeft 
                     animations:^{
                                     [myImageView1 setImage:image1];
                                 };
                     completion:^(BOOL finished){
                                       [self showImage2];
                                }];
}

-(void)showImage2 {
    [UIView animateWithDuration:5.0
                          delay:0.0 
                        options:UIViewAnimationOptionTransitionFlipFromLeft 
                     animations:^{
                                     [myImageView1 setImage:image2];
                                 };
                     completion:^(BOOL finished){
                                       ...
                                }];
}

答案 3 :(得分:0)

    NSMutableArray *imageArray = [[NSMutableArray alloc] init];

    for (int i = 0; i < 6; i++)
    {
        [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"ball%d.png", i]]];
        UIImageView *animatedImages = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,100,100)];
        animatedImages.animationImages = [NSArray arrayWithArray:imageArray];
        animatedImages.animationDuration = 1.0;
        animatedImages.animationRepeatCount = 0;
        [animatedImages startAnimating];
        [myview addSubview:animatedImages];  
        [animatedImages release];

     }