1

我是社区的新手,所以如果我的问题不清楚,请告诉我。我正在尝试在 iPAD 上进行选择反应练习。有两张图像应该随机出现在屏幕的左右两侧,用户将通过点击对应于出现图像位置的按钮来响应。这是问题所在,我尝试使用以下方式让这两个图像以随机顺序出现:

- (void) viewDidAppear:(BOOL)animated
{
   for(int n = 1; n <= 20; n = n + 1)
   {
      int r = arc4random() % 2;
      NSLog(@"%i", r);
      if(r==1)
      {
        [self greenCircleAppear:nil finished:nil context: nil];
      }
      else
     {
        [self redCircleAppear:nil finished:nil context: nil];
     }
  }
}

但是,仅运行一组动画时会生成 20 个随机数。有没有办法让动画在下一个循环开始之前在每个循环中完成运行?任何帮助表示赞赏,在此先感谢!

4

1 回答 1

0

当您说“只运行一组动画”时,我假设这意味着greenCircleAppearredCircleAppear开始出现图像序列并且用户按下按钮。如果是这种情况,我建议不要使用for循环,viewDidAppear而是viewDidAppear初始化当前状态并调用呈现下一个动画的方法。动画完成后,让它调用呈现下一个动画的方法。这些方面的东西:

将此添加到界面:

@interface ViewController ()

@property NSInteger currentIteration;

@end

这是在实现中:

- (void)viewDidAppear:(BOOL)animated {
    self.currentIteration = 0;
    [self showNextAnimation];
}

- (void)greenCircleAppear:(id)arg1 finished:(id)arg2 context:(id)arg3 {
    //perform animation
    NSLog(@"green");
    [self showNextAnimation];
}

- (void)redCircleAppear:(id)arg1 finished:(id)arg2 context:(id)arg3 {
    //perform animation
    NSLog(@"red");
    [self showNextAnimation];
}

- (void)showNextAnimation {
    self.currentIteration = self.currentIteration + 1;
    if (self.currentIteration <= 20) { //you should replace '20' with a constant
        int r = arc4random() % 2;
        NSLog(@"%i", r);
        if(r==1)
        {
            [self greenCircleAppear:nil finished:nil context: nil];
        }
        else
        {
            [self redCircleAppear:nil finished:nil context: nil];
        }
    }
    else {
        //do what needs to be done after the last animation
    }
}
于 2013-01-08T16:24:25.143 回答