Search code examples
objective-cioscocoacore-animation

Why should I use the block-based animation rather than begin/end animation?


As you know, Apple encourages us to use a new method called block-based animation about animation over iOS 4.0.

I really wonder what block-based animation is better than begin/end style animation.
    performance?
    concurrency?
    coding efficiency and convenience?


Solution

  • I wondered about this too back then.

    But after using block based animations like this:

    [UIView animateWithDuration:0.5 ... ^{
        // animated custom view vertically
    } completion:^{
        [UIView animateWithDuration:0.5 ... ^{
            // animate the fade in alpha of buttons
        }];
    }];
    

    It provides the completion handler in a short concise manner. You can also nest sub animation blocks within each other.

    With BeginAnimation/EndAnimation, I don't recall exactly how to do a callback for completion handler, but you usually do something like:

    // begin animation // set delegate // create delegate callback function for each beginAnimation

    Now imagine if you wanted to nest 3 or 4 levels animation, such as as replicating the CSS Lightbox effect:

    1) Fade in Lightbox container

    2) Expand Width

    3) Expand Height

    4) Fade in form

    You'd have to deal with some pretty messy if-else condition.

    Your workflow would be like:

    "After this beginAnimation finish, it sends a message to my callback method, scrolls down Xcode to find the callback delegate method, then in the callback method it calls another UIView beginAnimation, scroll back up Xcode to find the next beginAnimation ... "

    With block based animation, each process is encapsulated in a block which you can nest in another block. If you decided you want to change the order things appear such that:

    1) Fade in Lightbox container

    2) Expand Height before Width this time

    3) Expand Width after height this time

    4) Fade in form

    With the beginAnimation approach, you'll start pulling your hairs out.

    Hope that helps.