2
votes

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.

1
So decouple it then! Seriously? I'm not writing your code for you. I'm suggesting ways to get around it. What is stopping you from calling a method on your game logic class to determine which method to call next? I put an if block in there to show you that you can put actual code in there. That if isn't set in stone. You can choose many different solutions. You should be able to do this now.Fogmeister

1 Answers

2
votes

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.