调用多个动画,一个接一个同一个UIView对象,不使用完成 [英] Call multiple animation, one after another one same UIView object, without using completion

查看:27
本文介绍了调用多个动画,一个接一个同一个UIView对象,不使用完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UPATE 开始

正确答案在这里:
是否可以做多个不使用完成块的 UIView 动画

无需阅读此内容.

UPATE 结束

我有与 UIView animateWithDuration 立即返回 类似的问题,但我不能使用完成块因为我的动画具有不同的功能.

但确实想为同一个对象设置动画.

我正在制作纸牌游戏,所以我在屏幕上移动纸牌,但在移动之间我也有一些游戏逻辑.这就是为什么我可以方便地单独制作动画.

如何解决这个问题?

@Fogmeister
完成问题如下:
在method1之后,method2被调用.
如果我想调用method1 52次(因为我有52张卡),那么method2也会被调用52次.
但在我的场景中,我需要遵循,调用 method1 52 次,而不是调用 method2 4 次...
所以我不明白这怎么会因为竞争而下降.
另外有时我需要在method1之后调用method2,但有时我需要在method1之后调用method3...

我想在method1之后对对象进行深拷贝,然后在new对象上调用method2.
这行得通吗?

@Fogmeister 第二个回复
我也得出了同样的结论.
但由于以下原因,我不喜欢它.
我需要把游戏逻辑放在动画逻辑中,我希望事情是松散耦合的.
动画代码不应该处理游戏逻辑代码.

我想做的是做多个动画,一个接一个,同一个UIView对象,不使用completion:,因为我不会让动画代码保持简单并重复使用.>

如果你愿意,你可以从
下载我的代码http://weborcode.com/dl/Tablic.zip
动画在 TBL_CardView.m 文件方法动画中完成*
它是从以下位置调用的:
TBL_GameViewController.m 文件方法 gameStateMachineLoop所有卡从左到右洗牌,然后再将一张卡发送到左.
但问题是那张牌在洗牌前已经在右边了,如果你开始这个项目,你就会看到它.

解决方案

实际上没有很多信息要继续,但您不必为每个动画制作一个动画.如果要为所有卡片设置动画,请使用单个动画并更新 animations 块中的所有卡片.

如果你想为所有卡片制作动画,然后做其他事情,那么你可以做这样的事情......

- (void)animateCards:(NSArray *)cards{//调用一次,数组中的所有卡片都将动画超过 5 秒.[UIView animateWithDuration:5.0动画:^{对于(卡*卡中卡){card.frame =//随便}}完成:^(布尔完成){如果(iNeedToCallMethod2){[self doSomethingElse];} 别的 {[自己的东西完全不同];}}];}- (void)doSomethingElse{//这只会在所有卡片都动画后运行//如果条件要求它运行[UIView animateWithDuration:5.0动画:^{for (Suit *suit in self.suits) {//不太确定你想在这里做什么//但你可以做和上面一样的}}完成:^(布尔完成){}];}- (void)somethingCompletelyDifferent{//只有在其他方法没有运行时才会运行一次}

您所做的就是控制流量.不要再考虑在桌子周围移动卡片了,而是更多地考虑您有哪些可用的工具以及如何使用它们让事情按您的意愿行事.

UPATE start

Correct answer is here:
Is it posible to do multiple animations on UIView without using completion block

No need to read this.

UPATE end

I have similar problem as UIView animateWithDuration returns immediately, but I can not use completion block because my animations are in different functions.

But do want to animate same object.

I am making card game, so I am moving card across the screen, but betwean moving I also have some game logic. So that why it is convenient for me to do animations as separate.

How to solve this ?

@Fogmeister
Problem with completion is following:
after method1, method2 is called.
If I want to call method1 52 times (because I have 52 cards), then method2 will also be called 52 times.
But in my scenario I need following, call method1 52 times, than call method2 4 times...
So I do not see how this can be down with competition.
Also sometimes I need to call method2 after method1, but sometimes I need to call method3 after method1...

I was thinking to make deep copy of objects after method1, and then on new object to call method2.
Will that work ?

@Fogmeister 2nd response
I have also come to same conclusion.
But I do not like it duo to following reasons.
I need to put game logic in animation logic, and I would like thing to be loosely coupled.
Animation code should not deal with game logic code.

What I want to do do is to do multiple animation, one after another one same UIView object, without using completion:, because I wont to keep animation code simple and reuse it also.

If you want you can download my code from
http://weborcode.com/dl/Tablic.zip
Animation is done in TBL_CardView.m file method animation*
and it is called from:
TBL_GameViewController.m file method gameStateMachineLoop all card are shuffled from left to right, and then one card is send to left again.
But problem is that that card is already on the right before shuffle, If you start the project you will se it.

解决方案

There isn't really a lot of info to go on but you don't have to animate one things per animation. If you want to animate all the cards then use a single animation and update all the cards in the animations block.

If you want to animate all the cards and then something else then you could do something like this...

- (void)animateCards:(NSArray *)cards
{
    // call this once and all of the cards in the array will animate over 5 seconds.
    [UIView animateWithDuration:5.0
                     animations:^{
                         for (Card *card in cards) {
                             card.frame = // whatever
                         }
                     }
                     completion:^(BOOL finished) {
                         if (iNeedToCallMethod2) {
                             [self doSomethingElse];
                         } else {
                             [self somethingCompletelyDifferent];
                         }
                     }];
}

- (void)doSomethingElse
{
    //this will only run after all the cards have animated
    //if the condition requires it to run

    [UIView animateWithDuration:5.0
                     animations:^{
                         for (Suit *suit in self.suits) {
                             // not really sure what you want to do here
                             // but you can do the same as above
                         }
                     }
                     completion:^(BOOL finished) {
                     }];
}

- (void)somethingCompletelyDifferent
{
    //this will run once only if the other method doesn't run
}

All you are doing is controlling the flow. Stop thinking about moving cards around a table and think more about what tools you have available and how you can use them to make things do what you want.

这篇关于调用多个动画,一个接一个同一个UIView对象,不使用完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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