Nested animation in iOS: combine animated and non-animated code in animateWithDuration?

611 views Asked by At

Nested animation

In short the nested animation allows to run 2 independent animations simultaneously. But what to do if I have a complex code which consists of both animated and non-animated code? I mean the following situation:

[UIView animateWithDuration:...
animations:^{

    ...//code1 - with animation
    ...//code2 - also contains code which shouldn't be animated
}];

Code2 looks like the calling of the following method:

- (void)someMethod {

    ...//code3 - without animation
    [self someMethod2Animated:NO completion:^{

        [self someMethod2Animated:YES completion:^{

            NSLog(...);
        }];
    }];
}

Where someMethod2 executes the same code but maybe inside the animatedWithDuration:animations:completion: depending on the given BOOL variable.

If I run this code then I see that all the code parts are animated. I tried to set the durations of the code2 to zero but it has no effect (the code is still animated).

Could you suggest how to solve this issue without of rewriting all the code? I heared about some solutions like ReactiveCocoa but it seems they are suitable for network/separate asynchronous requests only.

Edited

PromiseKit 2.0 allows to convert enclosed animation blocks to a sequence of blocks but it doesn't support iOS 7 I need.

2

There are 2 answers

0
Vyachaslav Gerchicov On BEST ANSWER

The best solution I found out is RZViewActions. Advantages for my case:

  • operate with animations as with objects without nested blocks;

  • serial and parallel animations are "sequences" and "groups" which are objects too created from simple arrays of animations;

  • just take its demo, remove [self.spinner startAnimating];, run and click the screen multiple times. A lot of animations are performed simultaneously even without any special code (in case of animateWithDuration: the next animation would kill the previous unfinished one).

The second solution could be PromiseKit 2.0. It may be run under iOS 7 but it needs some magic to be added to the project.

4
Dan Loughney On

UIView animateWithDuration is great for very simple (non-interactive) animations. For what you are doing, you should dive into CAAnimation. UIView animateWithDuration will animate any view changes which occur anywhere within the block.

You are basically looking to conditionally chain your animations. Here's a good answer about chaining animations. How to chain different CAAnimation in an iOS application