5

我正在尝试制作一个简单的西蒙游戏(不断增加的颜色闪烁序列,用户必须尝试重复该序列)。我要解决的方法是拥有 4 个 UIViews 和 8 个图像。所以有一个用于红色、蓝色、绿色和黄色的 UIView。我有深绿色、浅绿色、深红色、浅红色等的 GIF。

所以在我的代码中,我使用 UIView 的 transitionWithView 动画块: UIView imageView.image 默认为深色图像,这个块正在转换为浅色图像,完成后返回深色图像(给它“点亮”的样子)。

- (void) animateColor:(NSString *)color
{
    ...
    //do stuff to default the dark and light image name and which UIView to animate based on the color parameter
    ...
    [UIView transitionWithView:imageView
                  duration:.5
                   options: UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAutoreverse
                animations:^{
                    imageView.image = [UIImage imageNamed:lightImage];
                } 
                completion:^(BOOL finished){
                    if (finished){
                        imageView.image = [UIImage imageNamed:darkImage];
                    }
                }];
}

当我用一种颜色调用此代码的方法时,这很好用。

问题是,假设我有一个 NSArray 或想要按顺序制作动画的颜色。因此,如果我的数组是“蓝色”、“绿色”,我希望使用蓝色调用 transitionWithView,为蓝色视图设置动画,然后为绿色​​视图设置动画。现在,它将同时为绿色视图和蓝色视图设置动画。

我现在正在尝试使用 NSTimer,但运气不佳。

任何建议都将不胜感激。

4

2 回答 2

5

当我尝试连续执行多个转换时,我遇到了类似的问题。我通过简单地在完成块中设置一个状态变量来解决它。然后此状态变量启用下一次转换的条件。好吧,因为我使用的是带有 runloop 方法的计时器,所以我的工作在这里更容易:

- (void) runloop {

    static int step = 1;

    if (step == 1)
    {
        step = 0;
        [UIView transitionWithView:imageView
                          duration:1
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{ imageView.image = [UIImage imageNamed:lightImage]; }
                        completion:^(BOOL finished){ step = 2; }];
    }

    if (step == 2)
    {
        step = 0;
        [UIView transitionWithView:imageView
                          duration:1
                           options:UIViewAnimationOptionTransitionCrossDissolve
                        animations:^{ imageView.image = [UIImage imageNamed:darkImage]; }
                        completion:^(BOOL finished){ step = 0; }];
    }
}

通过这种方式,您可以进行数十次转换,而不会迷失在嵌套块中。

于 2012-10-08T20:02:07.730 回答
3

好的,所以在玩了一些/很多之后,我找到了解决方案。首先,我将 transitionWithView 更改为如下所示:

[UIView transitionWithView:imageView
                  duration:1
                   options: UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                    imageView.image = [UIImage imageNamed:lightImage];
                } 
                completion:^(BOOL finished){
                    if (finished){
                        [UIView transitionWithView:imageView
                                          duration:1
                                           options:UIViewAnimationOptionTransitionCrossDissolve
                                        animations:^{
                                            imageView.image = [UIImage imageNamed:darkImage];
                                        }
                                        completion:^(BOOL done){
                                            if (done){
                                                // Do some program upkeep logic 
                                            }
                                        }
                         ];
                    }
                }
 ];

上面最大的变化是删除了我原来的 UIViewAnimationOptionAutoreverse,并在完成块中添加了从浅色图像到深色图像的动画。

然后调用上述方法 (animateColor:),我循环遍历一个数组并使用具有递增 afterDelay 的执行选择器:

    for (NSString *color in self.randomPattern.patternArray) {
        [self performSelector:@selector(animateColor:) withObject:color afterDelay:1.5*self.counter];
        ++self.counter;
    }
于 2012-07-09T02:09:17.963 回答