-->

目标c:执行多个transitionWithView动画块(objective-c: executi

2019-09-18 22:05发布

我试图做一个简单的西蒙游戏(颜色闪光的递增序列和用户尝试重复序列)。 我将它的方式是通过聚合具有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,但不具有带多少运气。

任何意见,在那里,将不胜感激。

Answer 1:

我有一个类似的问题,当我试图连续执行多个过渡。 我通过简单的设置完成块中的状态变量解决了这个问题。 然后该状态变量使能紧接着转移的条件。 那么,既然我使用一个计时器与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; }];
    }
}

这种方式可以使几十个过渡的不迷路到嵌套块。



Answer 2:

好了,周围的一些上场后/很多我找到了解决办法。 首先,我改变了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;
    }


文章来源: objective-c: executing multiple transitionWithView animation blocks