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

只谈情不闲聊 提交于 2019-12-07 14:39:33

问题


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?


回答1:


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.




回答2:


Largely convenience.

There's little in the way of performance improvements that can be made from using a block. So it's unlikely anything in that. I'd have thought that all the block syntax for animations does is to call through to the old methods (or effectively do that through calling similar internal methods) and just run the block in between where you do beginAnimation and commitAnimation.

So, convenience. I suggest using it for that reason alone anyway. It's a lot easier to use and makes it easy to nest animations and do things on completion as you don't need to create other methods to call upon completion - it's just another block.



来源:https://stackoverflow.com/questions/13135096/why-should-i-use-the-block-based-animation-rather-than-begin-end-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!