我想在特定时间内制作动画。我的代码如下:
- (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];
}
现在的问题是在播放animationDuration之后,animationRepeatCount会持续几秒钟!但我想在4秒内制作我的animationRepeatCount。 4秒后我的动画应该停止!我正在做我的代码有什么问题?或者,我该如何解决这个问题?
答案 0 :(得分:2)
在继续之前,您需要了解以下内容:
animationDuration; //一个周期的图像。默认值是图像数量* 1/30秒(即30 fps)
这意味着您设置的动画持续时间是针对一个图像循环。所以现在如果你想在4秒内重复动画4次。设置animationDuration
1秒。
如果需要,您也可以在任何情况下手动stopAnimating
;
答案 1 :(得分: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];
}
答案 2 :(得分: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.