-2

我想在特定的时间内制作动画。我的代码如下:

- (void)animate{

NSArray *myImages = [[NSArray alloc] initWithObjects:
                     [UIImage imageNamed:@"01.png"],
                     [UIImage imageNamed:@"02.png"],
                     [UIImage imageNamed:@"03.png"],
                     [UIImage imageNamed:@"04.png"],
                     [UIImage imageNamed:@"05.png"],
                     [UIImage imageNamed:@"06.png"],
                     [UIImage imageNamed:@"07.png"],
                     [UIImage imageNamed:@"08.png"],
                     [UIImage imageNamed:@"09.png"],
                     [UIImage imageNamed:@"10.png"],
                     [UIImage imageNamed:@"11.png"],
                     [UIImage imageNamed:@"12.png"],
                     [UIImage imageNamed:@"13.png"],
                     nil];


faceImage.animationImages = myImages;
faceImage.animationDuration = 4.0;
faceImage.animationRepeatCount = 4;

[faceImage startAnimating];

}

现在的问题是经过动画持续时间后,animationRepeatCount 会再持续几秒钟!但我想在那 4 秒内制作我的动画重复计数。4 秒后我的动画应该停止了!我在做我的代码有什么问题?或者,我该如何解决这个问题?

4

3 回答 3

2

在继续之前,您需要了解以下内容:

动画持续时间;// 一个周期的图像。默认为图像数量 * 1/30 秒(即 30 fps)

这意味着您设置的动画持续时间是一个图像周期。所以现在如果你想在 4 秒内重复动画 4 次。设置animationDuration1 秒。

stopAnimating如果需要,您也可以在任何情况下手动操作;

于 2013-05-07T07:10:11.643 回答
1

您的代码应该是:

- (void)animate
{
   NSArray *myImages = [[NSArray alloc] initWithObjects:
                     [UIImage imageNamed:@"01.png"],
                     [UIImage imageNamed:@"02.png"],
                     [UIImage imageNamed:@"03.png"],
                     [UIImage imageNamed:@"04.png"],
                     [UIImage imageNamed:@"05.png"],
                     [UIImage imageNamed:@"06.png"],
                     [UIImage imageNamed:@"07.png"],
                     [UIImage imageNamed:@"08.png"],
                     [UIImage imageNamed:@"09.png"],
                     [UIImage imageNamed:@"10.png"],
                     [UIImage imageNamed:@"11.png"],
                     [UIImage imageNamed:@"12.png"],
                     [UIImage imageNamed:@"13.png"],
                     nil];


   faceImage.animationImages = myImages;
   faceImage.animationDuration = 1.0;
   faceImage.animationRepeatCount = 4;

   [faceImage startAnimating];
}
于 2013-05-07T07:11:28.663 回答
0

首先用这样的东西替换你糟糕的数组初始化:

int imageCount = ...;
int *longestStringLength = [NSString stringWithFormat:@"%d", imageCount - 1].length;
NSMutableArray *myImages = [[NSMutableArray alloc] init];
for (int i = 0; i < imageCount; i++) {

    NSString *filename = [NSString stringWithFormat:@"%d", i];
    while(filename.length < longestStringLength) {

        filename = [NSString stringWithFormat:@"0%@", filename];
    }
    filename = [filename stringByAppendingString:@".png"];
    [myImages addObject:[UIImage imageNamed:filename]];
}

其次,检查您当前的动画持续时间,如有必要,将其变量除以 4。

于 2013-05-07T07:53:54.467 回答