ios - 在 xcode 中第一次运行时动画卡顿

标签 ios xcode animation preload

我已经尝试在这里查看至少六个问题,但我似乎无法弄清楚如何正确预加载图像。每个动画的第一次运行总是滞后到我可能丢失 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/

相关文章:

ios - 我怎样才能正确识别用户修改了哪些单元格的 subview ?

ios - 我如何分离这些图像选择器功能?

python - 如何在每次构建 Xcode 之前运行 Python 脚本?

javascript - 在 React 中进行 dom 操作的最优化方式

ios - 在 swift4 中向 UIView 添加云阴影

ios - 如何在 TableView 中查找已编辑文本字段的索引

ios - Apple Iphone 的 3.5 英寸或 4 英寸设计

ios - 切换到 XCode 7.0(.1) 后,自定义 View (XIB) 在 Release模式下崩溃

JQuery 动画中 stop() 和 Delay() 的问题

ios - 如何在 SWIFT 3 中使用按钮为 3 个或更多 View Controller 之间的转换设置动画?