iOS7 SpriteKit如何在恢复方法执行之前等待动画完成? [英] iOS7 SpriteKit how to wait for an animation to complete before resuming method execution?

查看:73
本文介绍了iOS7 SpriteKit如何在恢复方法执行之前等待动画完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以让方法等待方法内启动的SpriteKit操作完成,然后再继续执行代码?这是到目前为止的内容,但是它只是挂在等待循环上.

Is there a way to make a method wait for a SpriteKit action initiated within the method to complete before continuing code execution? Here's what I have so far, but it just hangs at the wait loop.

    __block BOOL  wait = YES;

    SKAction* move = [SKAction moveTo:destination duration:realMoveDuration];
    SKAction* sequence  = [SKAction sequence:@[[SKAction waitForDuration:0.07],move,[SKAction waitForDuration:0.07] ]];

    [spellEffect runAction:sequence  completion:^{
        [spellEffect removeFromParent];
        wait = NO;
    }];
    DLog(@"Waiting");
    while (wait) {

    }

    DLog(@"Done waiting");

推荐答案

我认为您对块的执行有误解,苹果提供了出色的文档:

I think you have a misunderstanding about the execution of blocks, apple has great docs: here. Your while loop will run infinitely, and even if the block did stop it, using that type of system to block your main thread will likely get your app shot down during review.

您应继续在该区域内.例如,如果您想继续使用名为continueAfterAnimationIsDone的方法,则可以

You should continue within the block. If for instance, you wanted to continue with a method called continueAfterAnimationIsDone, you could do

// Runs first
[spellEffect runAction:sequence  completion:^{
    [spellEffect removeFromParent];
    wait = NO;
    // Done
    // Runs third
    [self continueAfterAnimationIsDone]; // this method won't start until animations are complete.
}];
// Runs second

请注意,在声明块时,将捕获其中的代码,并且动画将从后台线程开始.其余代码将继续,因此您正在等待的循环将开始.动画完成后,该块便会执行,但它无法正确通知您的while循环,因此循环永远不会结束.让我知道,如果您还有其他问题,方块可能会很奇怪.

Note that when you are declaring the block, the code within it is captured and animations begin on a background thread. The rest of your code continues, so you're waiting loop will start. Once animations are completed this block executes, but it doesn't have a way to properly notify your while loop and so the loop never ends. Let me know if you have further questions, blocks can be strange.

这篇关于iOS7 SpriteKit如何在恢复方法执行之前等待动画完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆