IOS - multiple blocks of animation?

I have several animations that need to be run as a chain. The way I have dealt with this is to use executeHandler and run the next block of animation. Is there a cleaner way to handle this?

[UIView animateWithDuration:1 animations^{

        // perform first animation
     }completion:(BOOL finished){

          [UIView animateWithDuration:1 animations^{

               // perform second animation
          }completion:(BOOL finished){

         }];

}];

      

+3


source to share


2 answers


You can also use animateWithDuration:delay:options:animations:completion:

staggered intervals so that they start in order, but it's usually best to do them with completion blocks.

If there are multiple of them and it makes the code difficult to read, just undo the blocks (I am typing this from the top of my head so it cannot compile):



typedef void (^AnimationBlock)();

AnimationBlock firstAnimation = ^{ ... };
AnimationBlock secondAnimation = ^{ ... };

[UIView animateWithDuration:1 animations:firstAnimation completion:(BOOL finished) {
  [UIView animateWithDuration:1 animations:secondAnimation];}];

      

You can create a category on UIView

that took an array of such blocks and pinned them together for you, but then you have to deal with all corner cases. How do you determine timings; how do you deal with interrupted animation, etc. The above is probably the best approach in most cases.

+10


source


I wrote a blog post about this. My approach is to create an animation object that encapsulates the block and other animation properties. You can then pass an array of such objects to another animation object that will execute them sequentially. The code is available on GitHub .



+1


source







All Articles