Apple introduced blocks recently. In the UIView animation documentation, the old way of doing animation is discouraged:
Use of this method is discouraged in iPhone OS 4.0 and later. You should use the block-based animation methods instead.
So what is block-based animation methods? Here is an example:
CGPoint originalCenter = icon.center;
[UIView animateWithDuration:2.0
animations:^{
CGPoint center = icon.center;
center.y += 60;
icon.center = center;
}
completion:^(BOOL finished){
[UIView animateWithDuration:2.0
animations:^{
icon.center = originalCenter;
}
completion:^(BOOL finished){
;
}];
}];
The above code will animate a UIImageView* icon in a 2-second animation. Once completed, another animation will move the icon back to it’s original position.
A sample project can be downloaded here.
About
3 Comments
What is the value of icon.center when the completion block is called? I ask because there is an example in the documentation that implies you don’t have to increment the value within the animations block.
Are you incrementing or is just going to move 60 px?
The example move the icon from the original postion by 60 points.
Hello, Horace,
Thank you very much indeed! Have a nice day in HK.
Berlin, Germany