我已经尝试过在这里查看至少六个问题,我似乎无法弄清楚如何正确预加载图像。第一次运行总是滞后于每个动画到我丢失大约25%的帧的程度。这不是最引人注目的事情,但它显然是显而易见的。在那之后,当他们装载它时,它不会再断断续续。它们是相对较短的动画,没有超过20个PNG。
我的图像数组是
{
Stickman.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"DAL1.png"],
[UIImage imageNamed:@"DAL2.png"],
[UIImage imageNamed:@"DAL3.png"],
[UIImage imageNamed:@"DAL4.png"],
[UIImage imageNamed:@"DAL5.png"],
[UIImage imageNamed:@"DAL6.png"],
[UIImage imageNamed:@"DAL7.png"],
[UIImage imageNamed:@"DAL8.png"],
[UIImage imageNamed:@"DAL9.png"],
[UIImage imageNamed:@"DAL11.png"],
[UIImage imageNamed:@"DAL11.png"],
[UIImage imageNamed:@"DAL12.png"],
[UIImage imageNamed:@"DAL13.png"],
[UIImage imageNamed:@"DAL14.png"],
[UIImage imageNamed:@"DAL15.png"],
[UIImage imageNamed:@"DAL16.png"],
[UIImage imageNamed:@"DAL17.png"],
[UIImage imageNamed:@"DAL18.png"],
[UIImage imageNamed:@"DAL19.png"],
[UIImage imageNamed:@"DAL20.png"],
nil];
[Stickman setAnimationRepeatCount:1];
Stickman.animationDuration = 1.52;
[Stickman startAnimating];
}
起初我以为是模拟器滞后,但是当我在真实设备上试用它时,它仍然存在同样的问题。
答案 0 :(得分:1)
1st - 你需要在startAnimation之前尝试加载图像数组(应用程序确实完成了,例如视图加载了)
- (void)viewDidLoad {
Stickman.animationImages = @[[UIImage imageNamed:@"DAL1"],
[UIImage imageNamed:@"DAL2"],
[UIImage imageNamed:@"DAL3"],
[UIImage imageNamed:@"DAL4"]];
[Stickman setAnimationRepeatCount:1];
Stickman.animationDuration = 1.52;
}
- (void)startYourAnimation {
[Stickman startAnimating];
}
第二 - 尝试使用计时器和自定义动画类。
- (void)startAnimation {
timer = [NSTimer timerWithTimeInterval:0.25f target:self selector:@selector(hideandview) userInfo:nil repeats:NO];
}
- (void)stopAnimation {
if (timer != nil) {
[timer invalidate];
timer = nil;
}
}
- (void)nextFrame {
frame++;
if (frame > frameNumber) {
frame = 0; // For cycle
}
Stickman.image = [UIImage imageNamed:[NSString stringWithFormat:@"DAL%d", frame]];
}
3 - 优化您的图像资源(维度,扩展名) - .jpeg对内存的要求不高