0

我想在应用程序启动时添加一些动画。在 iPad 模拟器上一切正常。但是当它在真正的 iPad 上运行时,框架会静止一段时间,然后开始动画。在所有帧显示之前,动画就完成了。这是我的代码

    - (void)viewDidAppear:(BOOL)animated
{
    NSArray *myImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"1.png"], 
                         [UIImage imageNamed:@"2.png"], 
                         [UIImage imageNamed:@"3.png"], 
                         [UIImage imageNamed:@"4.png"], 
                         [UIImage imageNamed:@"5.png"],
                         [UIImage imageNamed:@"6.png"],
                         [UIImage imageNamed:@"7.png"],
                         [UIImage imageNamed:@"8.png"],
                         [UIImage imageNamed:@"9.png"],
                         [UIImage imageNamed:@"10.png"],
                         [UIImage imageNamed:@"11.png"],
                         [UIImage imageNamed:@"12.png"],
                         [UIImage imageNamed:@"13.png"],
                         [UIImage imageNamed:@"14.png"],
                         [UIImage imageNamed:@"15.png"],
                         [UIImage imageNamed:@"16.png"],
                         [UIImage imageNamed:@"17.png"],
                         [UIImage imageNamed:@"18.png"],
                         [UIImage imageNamed:@"19.png"],
                         [UIImage imageNamed:@"20.png"],
                         [UIImage imageNamed:@"21.png"],
                         [UIImage imageNamed:@"22.png"],
                         [UIImage imageNamed:@"23.png"],
                         [UIImage imageNamed:@"24.png"],
                         [UIImage imageNamed:@"25.png"],
                         [UIImage imageNamed:@"26.png"],
                         [UIImage imageNamed:@"27.png"],
                         [UIImage imageNamed:@"28.png"],
                         [UIImage imageNamed:@"29.png"],
                         [UIImage imageNamed:@"30.png"],
                         [UIImage imageNamed:@"31.png"],
                         [UIImage imageNamed:@"32.png"],
                         [UIImage imageNamed:@"33.png"],
                         [UIImage imageNamed:@"34.png"],
                         [UIImage imageNamed:@"35.png"],
                         [UIImage imageNamed:@"36.png"],
                         [UIImage imageNamed:@"37.png"],
                         [UIImage imageNamed:@"38.png"],
                         nil];   

    //myAnimatedView is UIImageView outlet
    myAnimatedView.animationImages = myImages; 
    myAnimatedView.animationDuration = 3; 
    myAnimatedView.animationRepeatCount = 1; 
    [myAnimatedView startAnimating];

    //trigger animationDone method when animation is finished
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, myAnimatedView.animationDuration * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self animationDone];
    });
4

3 回答 3

2

I believe the problem is with the way you are trying to detect if the animation has finished. Your approach is not correct as it's just based on estimates about the animation.

The phone has much less cpu power than the emulator, which is using your Mac's CPU, so it's going to be slower at executing things around. So your animation is not yet finished, when you assume it has finished loading.

Furthermore, I don't understand the logic behind using Grand Central Dispatch as a simple timer, which is basically what you are doing.

I would change the logic to a simple timer, where I would just check the isAnimating value of the UIImageView.

So, get rid of this:

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, myAnimatedView.animationDuration * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        [self animationDone];
    });

and replace with this:

[self performSelector:@selector(checkAnimation) withObject:nil afterDelay:0.1];

And here is our checkAnimation function:

- (void)checkAnimation {
    if (animationView.isAnimating) {
        [self performSelector:@selector(checkAnimation) withObject:nil afterDelay:0.1];
    }
    else {
        [self animationDone];
    }
}
于 2013-03-13T09:27:17.037 回答
0

加载 38 张 1024x768 图像会扼杀你的记忆。较新的 iPad 可能能够处理它,但肯定会出现严重的滞后问题并可能崩溃。特别是在迷你和旧垫上。

考虑一个小视频。

于 2013-03-14T13:16:02.027 回答
0

尝试预加载图像,以便在第一次制作动画时没有延迟。

于 2013-03-13T09:03:35.740 回答