Different Objective C syntax for functiona call
-
19-04-2021 - |
Question
[UIView animateWithDuration:1.0 animations:^(void) {
im.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) {
[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationCurveEaseOut animations:^(void) {
im.alpha = 0.0;
} completion:^(BOOL finished) {
[im removeFromSuperview];
}];
}];
The code is for animating UIImageView
, that I know;
I want to know the calling mechanism, as I haven't seen this kind of function call for the very first time.
Mainly, What is ^(void)
and why im.transform = CGAffineTransformIdentity;
passed to it?
I have gone thorugh Apple docs, to find anything related to this function call, and I got it too, but I didn't get any idea from there; or I might have been to wrong section.
Can anyone here guide me to this?
Solution
This is called a block, and was introduced in iOS 4 and Mac OS X 10.6.
Here are some links where you can learn more about them:
- Apple's Documentation - Blocks Programming Topics
- Ask Big Nerd Ranch: Blocks in Objective-C
- Programming with C Blocks
- Introduction to Blocks in Objective-C – Part 1
The above example should read as follows:
// Start an animation over the next 1 second
[UIView animateWithDuration:1.0 animations:^(void) {
// For this animation, animate from the current value of im.transform back to the identity transform
im.transform = CGAffineTransformIdentity;
} completion:^(BOOL finished) { // At the completion of the first animation...
// Wait 1 second, then start another 1-second long animation
[UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationCurveEaseOut animations:^(void) {
im.alpha = 0.0; // Fade out im during this animation
} completion:^(BOOL finished) { // When you complete this second animation
[im removeFromSuperview]; // Remove im from its superview
}];
}];
So you will have one second where im animates the removal of its transform, a one second delay, and then a one second fadeout of im.