在Xcode中循环之间暂停

时间:2012-09-03 15:24:04

标签: xcode animation for-loop delay

我正在尝试在一个方法中创建一个for循环,该方法会在敌人转弯时将计算机控制的敌方玩家设置预定距离6次。目前使用下面的代码,敌人朝着玩家的角色移动,但是循环运行的速度太快,因此每次移动时不会使敌人动画,而只会激活最终的移动。

基本上我试图做的是在循环结束时导致短暂(.75秒)延迟以使循环减慢到可接受的量。我在互联网上搜索了这个信息的高低,我很惊讶我找不到答案。看起来它非常简单。任何帮助将不胜感激!

for (int i=0; i<6; i++) {
    // Enemy NE
    if (enemyZombie.center.x < orcIdle.center.x && enemyZombie.center.y > orcIdle.center.y){

        [UIView animateWithDuration:.75 animations:^{ enemyZombie.center = CGPointMake(enemyZombie.center.x + 42.5, enemyZombie.center.y - 30); }];
    }
    // Enemy NW
    if (enemyZombie.center.x > orcIdle.center.x && enemyZombie.center.y > orcIdle.center.y){

        [UIView animateWithDuration:.75 animations:^{ enemyZombie.center = CGPointMake(enemyZombie.center.x - 42.5, enemyZombie.center.y - 30); }];
    }
    // Enemy SE
    if (enemyZombie.center.x < orcIdle.center.x && enemyZombie.center.y < orcIdle.center.y){

        [UIView animateWithDuration:.75 animations:^{ enemyZombie.center = CGPointMake(enemyZombie.center.x + 42.5, enemyZombie.center.y + 30); }];
    }
    // Enemy SW
    if (enemyZombie.center.x > orcIdle.center.x && enemyZombie.center.y < orcIdle.center.y){

        [UIView animateWithDuration:.75 animations:^{ enemyZombie.center = CGPointMake(enemyZombie.center.x - 42.5, enemyZombie.center.y + 30); }];
    }
}

1 个答案:

答案 0 :(得分:2)

您可以使用UIView的选择器animateWithDuration:animations:completion:中的完成块以递归方式执行循环中的下一步:

-(void)moveEnemyZombieWithSteps:(int)steps
{
    // check for end of loop
    if (steps == 0) return;

    // Enemy NE
    if (enemyZombie.center.x < orcIdle.center.x && enemyZombie.center.y > orcIdle.center.y)
    {

        [UIView animateWithDuration:.75
                         animations:^{ enemyZombie.center = CGPointMake(enemyZombie.center.x + 42.5, enemyZombie.center.y - 30);}
                         completion:^(BOOL finished){[self moveEnemyZombieWithSteps:steps - 1];}];
    }
    // Enemy NW
    ...
}

//start moving
moveEnemyZombieWithSteps(6);

没有测试过代码,但你会明白: - )

相关问题