码迷,mamicode.com
首页 > 其他好文 > 详细

学会使用Block编程

时间:2015-02-27 13:39:12      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:ios   block   

1.Block是什么?

Block是一个C级别的语法以及运行时的一个特性,和标准C中的函数(函数指针)类似,可以看成一个语句块或者更多时候看成一个匿名函数。

+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations NS_AVAILABLE_IOS(4_0);
[ UIView animateWithDuration:10 animations:^{  
       NSLog(@"%@,",@"block");  
    }]; 

2.Block在什么情况下使用?

Block除了能够定义参数列表、返回类型外,还能够获取被定义时的词法范围内的状态(比如局部变量),并且在一定条件下(比如使用__block变量)能够修改这些状态。此外,这些可修改的状态在相同词法范围内的多个block之间是共享的,即便出了该词法范围(比如栈展开,出了作用域),仍可以继续共享或者修改这些状态。通常来说,block都是一些简短代码片段的封装,适用作工作单元,通常用来做并发任务、遍历、以及回调。

3.Block如何申明?(对比于c语言中的函数申明)

技术分享

4.如何调用Block?(类比于函数指针)

 int (*CFunc) (int a) //函数调用 
 int result = CFunc(10); 
 int (^BFunc) (int  a)  //Block调用 
 int result = BFunc(10); 

5.Block编程实例(实现回调)

#import <UIKit/UIKit.h>
/**
 *  点击按钮的回调
 *
 *  @param selectIndex selectIndex 点击的索引值
 */
typedef void(^DialogViewCompleteHandle)(NSUInteger selectIndex);

@interface ViewController : UIViewController

@property (nonatomic,copy) DialogViewCompleteHandle completeHandle;

- (void)showDialogViewWithHandle:(void (^)(NSUInteger selectIndex))handler;
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "ViewController.h"

@interface ViewController ()
{
    UIView *_view;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)showAlert:(id)sender {

    [self showDialogViewWithHandle:^(NSUInteger selectIndex) {
        if (selectIndex == 0) {
            NSLog(@"第一个按钮被点击了!");
        }else
        {
            NSLog(@"第二个按钮被点击了!");
        }
    }];

}

- (void)showDialogViewWithHandle:(void (^)(NSUInteger selectIndex)) handler
{
    _view = [UIView new];
    _view.center = CGPointMake(SCREEN_WIDTH /2, SCREEN_HEIGHT / 2);
    _view.bounds = CGRectMake(0, 0, SCREEN_WIDTH /2, SCREEN_HEIGHT /2);
    _view.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_view];
    CGRect viewFrame = _view.frame;

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button1.center = CGPointMake(viewFrame.size.width /2, viewFrame.size.height /2 - 50);
    button1.bounds = CGRectMake(0, 0, 60, 40);
    button1.tag = 0;
    [button1 setTitle:@"button1" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [_view addSubview:button1];

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button2.center = CGPointMake(viewFrame.size.width /2, viewFrame.size.height /2 + 50);
    button2.bounds = CGRectMake(0, 0, 60, 40);
    button2.tag = 1;
    [button2 addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
    [button2 setTitle:@"button2" forState:UIControlStateNormal];
    [_view addSubview:button2];

    _view.transform = CGAffineTransformMakeScale(0.01, 0.01);

    [UIView animateWithDuration:0.3 animations:^{
        _view.transform = CGAffineTransformMakeScale(1.0, 1.0);

    }];

    self.completeHandle = handler;
}

- (void)clickButton:(UIButton *)sender
{
    NSUInteger selIndex = sender.tag;

    if (_completeHandle) {
        _completeHandle(selIndex);
    }

    [self closeView];
}
- (void)closeView{
    [UIView animateWithDuration:0.3 animations:^{
        _view.transform = CGAffineTransformMakeScale(0.01, 0.01);
        [_view removeFromSuperview];
    }];
}
@end

技术分享

点击button1 和 button2 可以实现按钮的回调Block
技术分享

Demo下载地址

学会使用Block编程

标签:ios   block   

原文地址:http://blog.csdn.net/huxiaoqiao163/article/details/43966247

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!