user2545330 user2545330 - 8 months ago 60
iOS Question

Alternative to performSelector:withObject:afterDelay: for animating views consequantially

I have method

that consist a lot of
[self performSelector:@selector(myAnimation) withObject:nil afterDelay: 21 * 0.01];
with different method's and delays.

How can I refactor my code not to use
? I use it for make animation consequentially change.


You don't specify how you exactly perform the animations themselves, but with UIView animation blocks you can also specify a delay.(animateWithDuration:delay:options:animations:completion:).

Alternatively you can use NSTimerfor performing delays.

But for a detailed answer you should provide more details on how you perform your animation.


Using animation blocks is very straightforward...In the completion block (see code below) you can start the follow up animation (for series of animations). For parallel animation processing you just start multiple blocks of animations...there are different messages. If you don't need to handle the animation end, you can use the ones without completion block.

Here is an example:

[UIView animateWithDuration:2.0 delay:0.1 options:UIViewAnimationOptionCurveEaseInOut animations:^{

    // perform animation code here

} completion:^(BOOL finished) {

    // This code is performed after the animation is completed. E.g. you can start a new animation here
    // (for serial animation processing):