Objective-c:执行多个 transitionWithView 动画块 [英] objective-c: executing multiple transitionWithView animation blocks

查看:67
本文介绍了Objective-c:执行多个 transitionWithView 动画块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个简单的西蒙游戏(越来越多的颜色闪烁,用户必须尝试重复该序列).我要做的方法是使用 4 个带有 8 个图像的 UIView.所以有一个用于红色、蓝色、绿色和黄色的 UIView.我有深绿色、浅绿色、深红色、浅红色等的 gif.

I'm trying to make a simple Simon game (an increasing sequence of colors flash and the user has to try to repeat that sequence). The way I'm going about it is by having 4 UIViews with 8 images. So there's a UIView for red, blue, green, and yellow. And I have gifs of dark green, light green, dark red, light red, etc.

所以在我的代码中,我使用了 UIView 的 transitionWithView 动画块:UIView imageView.image 默认为深色图像,此块正在转换为浅色图像,并在完成后返回深色图像(使其看起来亮起来").

So in my code, I'm using UIView's transitionWithView animation block: The UIView imageView.image is defaulted to the dark color image and this block is transitioning to light colored image, and on completion going back to the dark color image (to give it a "lighting up" look).

- (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];
                    }
                }];
}

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

This works fine when I call it the method this code in with one color.

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

The problem is, say I have an NSArray or colors I want to animate sequentially. So if my array was "blue","green" I would want the transitionWithView to be called using blue, animate the blue view, then animate the green view. Right now, it will animate the green view and the blue view at the same time.

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

I'm trying to play around with NSTimer right now, but not having much luck with that.

如果有任何建议,我们将不胜感激.

Any advice out there would be much appreciated.

推荐答案

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

OK, so after playing around some/a lot I found a solution. First, I changed the transitionWithView to look like this:

[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,并在完成块中添加了从亮图像到暗图像的动画.

The big change above was removing the original UIViewAnimationOptionAutoreverse I had, and add the animation to go from the light image to the dark image in the completion block.

然后调用上面的方法 (animateColor:),我遍历一个数组并使用一个增加 afterDelay 的执行选择器:

Then to call the above method (animateColor:), I loop through an array and use a perform selector with an increasing afterDelay:

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

这篇关于Objective-c:执行多个 transitionWithView 动画块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆