animationArray仅播放最后一张图像

问题描述

| 我目前有这个方法buttonHit,它调用playAnimationToNumber接受一个int,然后依次使用它遍历循环播放每次迭代的图像数组,但是它似乎只在播放最后一个数组。 ?因为我有点迷茫,不胜感激。
//attached to button and it calls animation method.
- (void)buttonHit:(id)sender{
    [self playAnimationToNumber:5];
}



- (void)playAnimationToNumber:(int)number{

    for (int counter=1; counter<=number; counter++) {

        Nsstring *imageNameForFirstNumber = [Nsstring stringWithFormat:@\"Flap%i.png\",counter];
        NSArray *imagesForAnimation = [NSArray arrayWithObjects:[UIImage imageNamed:@\"FlapMoving1.png\"],[UIImage imageNamed:@\"FlapMoving2.png\"],[UIImage imageNamed:imageNameForFirstNumber],nil];


        animationArray.animationImages = [[NSArray alloc] initWithArray:imagesForAnimation];        

        animationArray.animationDuration = 0.5;
        animationArray.animationRepeatCount = 1;
        [animationArray startAnimating];
        [self.view addSubview:animationArray];



    }


    [animationArray release];
}
工作代码 \' -(void)playAnimationToNumber:(int)number {
NSMutableArray *imagesForAnimation = [[NSMutableArray alloc] initWithCapacity:0];


for (int counter=1; counter<=number; counter++) {

    Nsstring *imageNameForFirstNumber = [Nsstring stringWithFormat:@\"Flap%i.png\",counter];
    [imagesForAnimation addobject:[UIImage imageNamed:@\"FlapMoving1.png\"]];
    [imagesForAnimation addobject:[UIImage imageNamed:@\"FlapMoving2.png\"]];
    [imagesForAnimation addobject:[UIImage imageNamed:imageNameForFirstNumber]];
}
animationArray.animationImages = [NSArray arrayWithArray:imagesForAnimation];        

animationArray.animationDuration = 5.9;
animationArray.animationRepeatCount = 1;
[animationArray startAnimating];
[self.view addSubview:animationArray];
[imagesForAnimation release];   
} \'     

解决方法

        在这里,您可以尝试此代码。
   - (void)playAnimationToNumber:(int)number{

    NSMutableArray *imagesForAnimation = [[[NSMutable alloc] initWithCapacity:0] autoRelease];
    [imagesForAnimation addObject:[UIImage imageNamed:@\"FlapMoving1.png\"]];
    [imagesForAnimation addObject:[UIImage imageNamed:@\"FlapMoving2.png\"]];

        for (int counter=1; counter<=number; counter++) {

            NSString *imageNameForFirstNumber = [NSString stringWithFormat:@\"Flap%i.png\",counter];
            [imagesForAnimation addObject:[UIImage imageNamed:imageNameForFirstNumber]];
    }
            animationArray.animationImages = [NSArray arrayWithArray:imagesForAnimation];        

            animationArray.animationDuration = 0.5;
            animationArray.animationRepeatCount = 1;
            [animationArray startAnimating];
            [self.view addSubview:animationArray];



        }
}