Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从循环内更改背景颜色?[ios]_Ios_Objective C - Fatal编程技术网

从循环内更改背景颜色?[ios]

从循环内更改背景颜色?[ios],ios,objective-c,Ios,Objective C,我试图通过循环一系列数值来改变背景色,每个数值对应一种颜色。对于列表中的每个整数,我希望背景更改为该颜色,暂停一秒钟,然后更改为列表中的下一种颜色。但是,即使整个列表正在迭代,背景颜色似乎只会更改为列表末尾的颜色。我可以做些什么来改变列表中每个项目的背景 当用户按下播放按钮时,执行以下代码: - (IBAction)play:(id)sender { [self.mem addObject: [NSNumber numberWithInt: arc4random() % 3]];

我试图通过循环一系列数值来改变背景色,每个数值对应一种颜色。对于列表中的每个整数,我希望背景更改为该颜色,暂停一秒钟,然后更改为列表中的下一种颜色。但是,即使整个列表正在迭代,背景颜色似乎只会更改为列表末尾的颜色。我可以做些什么来改变列表中每个项目的背景

当用户按下播放按钮时,执行以下代码:

- (IBAction)play:(id)sender {
    [self.mem addObject: [NSNumber numberWithInt: arc4random() % 3]];

    [UIView animateWithDuration:.5 delay:.5 options:UIViewAnimationOptionTransitionFlipFromRight
                 animations:
     ^(void) {
         for (int i = 0; i < [self.mem count]; i++) {
             if ([self.mem objectAtIndex:i] == [NSNumber numberWithInt:0]) {
                 self.view.backgroundColor = [UIColor blueColor];
             } else if ([self.mem objectAtIndex:i] == [NSNumber numberWithInt:1]) {
                 self.view.backgroundColor = [UIColor greenColor];
             } else if ([self.mem objectAtIndex:i] == [NSNumber numberWithInt:2]) {
                 self.view.backgroundColor = [UIColor redColor];
             } else {
                 self.view.backgroundColor = [UIColor cyanColor];
             }
         }
     }
                     completion:
     ^(BOOL finished) {
         if(finished){
         }
     }];
}
感谢您的帮助,谢谢

-animateWithDuration:options:animations:将使用您在动画块中最后设置的任何值执行动画-它不会在每次更改上花费持续时间秒


如果要设置背景从一种颜色到另一种颜色的动画,则需要分步骤进行。使用完成块触发另一个更改,可能只是再次调用-play:即可。您可能希望在ivar中保存一些状态,以便更容易地知道您在颜色序列中的位置。

您非常接近!我已经更新了你的方法,你应该能够简单地复制和粘贴它。有关解释,请参见@Caleb的答案

-(IBAction)play:(id)sender {
    static UIColor *color;
    // i is the ivar that saves the state in the color sequence
    static int i = 0;

    if (sender) {
        i = 0;
        [self.mem addObject:[NSNumber numberWithInt: arc4random() % 3]];
    }

    if ([self.mem objectAtIndex:i] == [NSNumber numberWithInt:0]) {
        color = [UIColor blueColor];
    } else if ([self.mem objectAtIndex:i] == [NSNumber numberWithInt:1]) {
        color = [UIColor greenColor];
    } else if ([self.mem objectAtIndex:i] == [NSNumber numberWithInt:2]) {
        color = [UIColor redColor];
    } else {
        color = [UIColor cyanColor];
    }

    [UIView animateWithDuration:0.5 delay:0.5 options:UIViewAnimationOptionTransitionFlipFromRight animations:^{
        self.backgroundColor = color;
    } completion:^(BOOL finished) {
        // Only continue animating if the play button has not been pressed
        if (i < self.mem.count - 1) {
            i += 1;
            // trigger another change by calling play:
            [self play:nil];
        }
    }];
}

如果延迟是基于i的值,那么这将工作得更好。否则所有动画都将同时运行。@rmaddy我不相信所有动画都会同时发生,因为在上一个动画完成之前不会调用该方法。回顾之后,我的示例实际上永远不会停止动画制作,因为每次调用都会在self.mem中添加一个对象,所以我永远不会超过self.mem.i。我不知道如何适当地更改此选项,因为我不知道blzn在每次按下按钮时都向数组添加值的原因,所以我现在就留下我的示例。Oops。我忽略了每个迭代都是从完成块调用的。没关系感谢@JustinMoser的代码,即使它永远不会结束,也非常有用!这是simon says类型游戏的开始,用户将尝试复制屏幕上显示的颜色组合。因此,每当用户成功复制颜色列表时,动画方法应再次运行,并向列表中添加额外的随机颜色。