objective-c:执行多个transitionWithView动画块

5 objective-c uiview uiviewanimation uiviewanimationtransition

我正在尝试制作一个简单的西蒙游戏(不断增加的颜色序列闪烁,用户必须尝试重复该序列).我正在进行的方式是通过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];
                    }
                }];
}
Run Code Online (Sandbox Code Playgroud)

当我用一种颜色称它为此代码的方法时,这工作正常.

问题是,我说我有一个NSArray或颜色,我想顺序动画.因此,如果我的数组是"蓝色","绿色",我希望使用蓝色调用transitionWithView,为蓝色视图设置动画,然后为绿色​​视图设置动画.现在,它将同时为绿色视图和蓝色视图设置动画.

我现在正试着玩NSTimer,但没有太多运气.

任何建议将非常感谢.

Jen*_*rth 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; }];
    }
}
Run Code Online (Sandbox Code Playgroud)

这样,您可以进行数十次转换,而不会迷失到嵌套块中.


小智 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 
                                            }
                                        }
                         ];
                    }
                }
 ];
Run Code Online (Sandbox Code Playgroud)

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

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

    for (NSString *color in self.randomPattern.patternArray) {
        [self performSelector:@selector(animateColor:) withObject:color afterDelay:1.5*self.counter];
        ++self.counter;
    }
Run Code Online (Sandbox Code Playgroud)