让两个图像以随机顺序iOS出现

时间:2013-01-08 15:39:47

标签: objective-c ios xcode

我是社区的新手,所以如果我的问题不清楚,请告诉我。我想在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个随机数,而只运行一组动画。有没有办法让动画在下一个循环开始之前在每个循环中完成运行?感谢任何帮助,提前谢谢!

1 个答案:

答案 0 :(得分: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
    }
}