For our developers, UIView animation has already been familiar to us. We can use this mechanism easily to implement awesome animation effects with very little code. A simple example like that:
Code:
viewToAnimate.alpha = 0;
[UIView animateWithDuration:1.0f animations:^{
viewToAnimate.alpha = 1;
}];
The code described above is a fade in effect. With UIView animation, we just need to use this 4 lines of code. The ^{...} syntax is Blocks introduced from iOS 4. Furthermore, the benefit of Blocks is more than that. For example, if the animation we needed is two or more steps, such as we want it fade in first and then zoom in. In traditional way, we should use the delegate of the completion of the animation and implement a method to do that . But with Blocks, it more straightforward:
Code:
viewToAnimate.alpha = 0;
[UIView animateWithDuration:1.0f
animations:^{
viewToAnimate.alpha = 1;
}completion:^(BOOL finished){
[UIView animateWithDuration:1.0f animations:^{
viewToAnimate.frame = CGRectMake(50, 50, 200, 200);
}];
}];
Our code using a completion Block to handle the complete of the animation. In the completion block, we create another animation block which zoom in our view. The result is that the view first fade in and then zoom in. With Blocks, we can do these all things in the same domain rather than separate them to divided methods. It could greatly improve our productivity specially for the animations which has serval steps.