把UIGestureRecognizer 中的点击事件变成Block

//
//  UIGestureRecognizer+Block.h
//  rrefwvenfo
//
//  Created by 张凯泽 on 16/4/11.
//  Copyright © 2016年 rytong_zkz. All rights reserved.
//

#import <UIKit/UIKit.h>
typedef void(^NVMGertureBlock)(id gesture);
@interface UIGestureRecognizer (Block)
+(instancetype)nvm_gestureRecongnizerWithActionBlock:(NVMGertureBlock)Block;
-(instancetype)initWithActionBlock:(NVMGertureBlock)Block;
@end
//
//  UIGestureRecognizer+Block.m
//  rrefwvenfo
//
//  Created by 张凯泽 on 16/4/11.
//  Copyright © 2016年 rytong_zkz. All rights reserved.
//
#import <objc/message.h>
#import "UIGestureRecognizer+Block.h"


static const int target_key;
@implementation UIGestureRecognizer (Block)
+(instancetype)nvm_gestureRecongnizerWithActionBlock:(NVMGertureBlock)Block
{
    return [[self alloc]initWithActionBlock:Block];
}
-(instancetype)initWithActionBlock:(NVMGertureBlock)Block
{
    self = [self init];
    [self addActionBlock:Block];
    [self addTarget:self action:@selector(invoke:)];
    return self;
    
}
-(void)addActionBlock:(NVMGertureBlock)block
{
    if (block) {
        objc_setAssociatedObject(self, &target_key, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
}
-(void)invoke:(id)sender
{
    NVMGertureBlock block = objc_getAssociatedObject(self, &target_key);
    if (block) {
        block(sender);
    }
}
@end

 

posted @ 2016-04-11 10:23  张凯泽  阅读(534)  评论(0编辑  收藏  举报