开发者

iPhone blocks explained

Hi all Im trying to understand how blocks work.

[UIView animateWithDuration:1 animations:^{
 blueViewController.view.alpha = 0;
 yellowViewController.view.alpha = 0;
}];

I would love to understand how they implemented this to make a transition. As i see it the block that updates the views's alpha to 0 can only be executed and开发者_JAVA技巧 that would result in an instant update and not a animation?. It would properly help me understand how blocks work if someone could explain to me how they might have implemented the function.

also i think its a bit wierd that i the closure created by the block only create readonly variables. How am i then able to change the alpha without defining it with the "_block" keyword? is the "_block" only required for scaler types? (int / float ect).


The magic is not the Block system, it just allows you to create and call anonymous functions. The magic is the Core Animation framework.

The basic is that all parameters are immediately applied, but instead of redrawing everything right away, Core Animation looks for the changes and applies them over the given animation duration.

Look into the Core Animation framework reference and the WWDC 2010 sessions 424 and 425 for deeper informations.


A block is just a kind of callback, that can be passed to another scope while still having access to the local variables, from the scope where it was defined. That's called closure.

You're question seems more related to CoreAnimation...


I think the other answers covered the distinction between blocks themselves and core animation and animation blocks (different use of the term "block").

But to your question about the captured variables, what you get is a const copy of the pointers 'blueViewController' and 'yellowViewController'. The pointers themselves are const copies, but you are using those pointers to access the underlying view controller objects to retrieve each view and then send each of those views a -[UIView setAlpha:] message.

If you spell it out, it may be more clear. Within your block you have:

UIView *view = [blueViewController view]; // <-- send view message to the const pointer
[view setAlpha:0.0]; // <-- Now send a setAlpha message to the view

blueViewController.view.alpha = 0.0 is simply shorthand for the above.

Hope that helps.


You can access any instance variables or global variables from blocks.

[UIView animateWithDuration:1 animations:^{
 blueViewController.view.alpha = blueViewController.myAlphaValue;
 yellowViewController.view.alpha = 0;
}];

blueViewController and yellowViewController are automatically retained from the block because these are Objective-C objects, it is safe to access from the block without worrying about instance's life span. If blueViewController and yellowViewController are ivar, only self is automatically retained by the block.

the __block storage type modifier is able to use for pointer as well.

__block MyViewController *myViewController = self.viewController;

However, this variable is not automatically retained from blocks even though this variable is Objective-C object. Be careful for life span of __block Objective-C object.

^{
    myViewController = self.viewController;
    /* If myViewController is with __block modifier, it is ok. */
    /* If myViewController is without __block modifier, this line is not able to compile. */
};

The __block Storage Type

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜