animationArray只播放最后一张图片

时间:2011-06-18 01:34:05

标签: iphone ios animation nsarray

我目前有这个方法buttonHit调用playAnimationToNumber,它接受一个int,然后用来运行循环播放每次迭代的图像数组,但它似乎只是在播放最后一个数组.. can你明白为什么?因为我有点迷失,并会感谢你的帮助。

//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];   

}“

1 个答案:

答案 0 :(得分:2)

在这里试试这段代码。

   - (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];



        }
}