这里的闭包自然是计算机语言中的闭包,词法闭包(Lexical Closure)的简称,主要表现在函数可以引用到自由变量,
准确地说在Objective-c里不叫闭包(Closure)
typedef int (^IntBlock)();
IntBlock downCounter(int start)
{<br> __block int i = start; return Block_copy( ^int() { return i--; }); <br>}<br><br>
IntBlock f = downCounter(5); printf("%d", f()); printf("%d", f()); printf("%d", f()); Block_release(f);
就是这么简单。
单纯就语法而言,闭包并没什么好谈的,但它的应用已经越来越广,
[self beginAnimation];
[self animationAction];
[self endAnimation];
而且每个动画都必需事先其专门的animationAction
+ (void)animation:(NSTimeInterval)duration withEvent:(animationEvent)even t
{
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationRepeatAutoreverses: NO];
event();
[UIView commitAnimations];
}
[Tools animation:0.8 withAnimationCurve:UIViewAnimationCurveEaseInOut withEvent:
^{
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRigh t
forView:centerView_ cache:YES];
// 移除功能列表
[menuView_ removeFromSuperview];
// 增加二次拔号键盘
[centerView_ addSubview:phonePad_ ];
}];