我已经尝试在这里查看至少六个问题,但我似乎无法弄清楚如何正确预加载图像。每个动画的第一次运行总是滞后到我可能丢失 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];
}
起初我以为是模拟器延迟,但是当我在真实设备上尝试时,它仍然有同样的问题。
最佳答案
1st - 你需要在 startAnimation 之前尝试加载图像数组(例如应用程序完成, View 加载)
- (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]];
}
第三 - 优化您的图像资源(尺寸、扩展名)- .jpeg 对内存的要求较低
关于ios - 在 xcode 中第一次运行时动画卡顿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30515345/