UIButton、UIGestureRecongnizer、UITapGestureRecongnizer、NSTimer、UISwitch事件以block的方式回调

实现的方式是给UIButton、UIGestureRecongnizer、UITapGestureRecongnizer、NSTimer、UISwitch 添加分类
在分类的.m实现文件中使用rumtime,objc_setAssociatedObject添加一个block属性,接收到事件后就会回调这个block
下载地址: https://github.com/boundlessocean/UIKit-Block-
1.UIButton
创建UIButton (BLBlock)的分类
初始化完成之后就可以直接调用bl_handleWithBlock:(BLButtonActionBlock)actionBlock controlEvent:(UIControlEvents)controlEvent处理事件就在block回调中啦

这是.h文件
#import 

typedef void (^BLButtonActionBlock)(id sender);
@interface UIButton (BLBlock)
/**
 *  处理事件
 *
 *  @param actionBlock  事件回调
 *  @param controlEvent 事件类型
 */
- (void)bl_handleWithBlock:(BLButtonActionBlock)actionBlock controlEvent:(UIControlEvents)controlEvent;

/**
 *  扩大 UIButton 的点击范围
 *  上下左右需要延伸的范围
 *
 *  @param top    <#top description#>
 *  @param right  <#right description#>
 *  @param bottom <#bottom description#>
 *  @param left   <#left description#>
 */
- (void)bl_setEnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left;
@end

---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------
这是.m

#import "UIButton+BLBlock.h"

#import 

static char topNameKey;
static char rightNameKey;
static char bottomNameKey;
static char leftNameKey;
static char kActionKey;

@implementation UIButton (BLBlock)

- (void)bl_handleWithBlock:(MFButtonActionBlock)actionBlock controlEvent:(UIControlEvents)controlEvent{
    
    objc_setAssociatedObject(self, &kActionKey, actionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(b_callActionBlock:) forControlEvents:controlEvent];
    
}

- (void)b_callActionBlock:(id)sender{
    
    MFButtonActionBlock block = (MFButtonActionBlock)objc_getAssociatedObject(self, &kActionKey);
    if (block) {
        block(self);
    }
}




- (void)bl_setEnlargeEdgeWithTop:(CGFloat)top right:(CGFloat)right bottom:(CGFloat)bottom left:(CGFloat)left
{
    objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (CGRect)b_enlargedRect
{
    NSNumber *topEdge = objc_getAssociatedObject(self, &topNameKey);
    NSNumber *rightEdge = objc_getAssociatedObject(self, &rightNameKey);
    NSNumber *bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);
    NSNumber *leftEdge = objc_getAssociatedObject(self, &leftNameKey);
    if (topEdge && rightEdge && bottomEdge && leftEdge) {
        return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,
                          self.bounds.origin.y - topEdge.floatValue,
                          self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,
                          self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
    }
    else
    {
        return self.bounds;
    }
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    CGRect rect = [self b_enlargedRect];
    if (CGRectEqualToRect(rect, self.bounds)) {
        return [super hitTest:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) ? self : nil;
}
@end

2.UIGestureRecognizer
同样调- (void)bl_handleRecognizerBlock:(BLGestureRecognizerBlock)actionBlock;在block中处理事件

这是.h
#import 

typedef void(^BLGestureRecognizerBlock)(id sender);

@interface UIGestureRecognizer (BLBlock)

- (void)bl_handleRecognizerBlock:(BLGestureRecognizerBlock)actionBlock;
@end
-------------------------------------
-------------------------------------
.m文件

#import "UIGestureRecognizer+BLBlock.h"
#import 

static char kGestureRecognizerKey;

@implementation UIGestureRecognizer (BLBlock)
- (void)bl_handleRecognizerBlock:(GestureRecognizerBlock)actionBlock{
    
    objc_setAssociatedObject(self, &kGestureRecognizerKey, actionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(b_callGestureRecognizer:)];
}

- (void)b_callGestureRecognizer:(id)sender{
    
    GestureRecognizerBlock block = (GestureRecognizerBlock)objc_getAssociatedObject(self, &kGestureRecognizerKey);
    if (block) {
        block(self);
    }
}
@end

3.UITapGestureRecongnizer

.h文件
#import 

typedef void (^BLTapGestureRecognizerBlock)(id sender);

@interface UITapGestureRecognizer (BLBlock)

- (void)bl_handleWithBlock:(BLTapGestureRecognizerBlock)actionBlock;
@end

----------------------------------------
----------------------------------------
.m实现文件
#import "UITapGestureRecognizer+BLBlock.h"
#import 
static char kTapGestureRecognizerKey;

@implementation UITapGestureRecognizer (BLBlock)

- (void)bl_handleWithBlock:(BLTapGestureRecognizerBlock)actionBlock{
    objc_setAssociatedObject(self, &kTapGestureRecognizerKey, actionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(b_callTapGestureRecognizerBlock:)];
    
}

- (void)b_callTapGestureRecognizerBlock:(id)sender{
    
    BLTapGestureRecognizerBlock blcok = (BLTapGestureRecognizerBlock)objc_getAssociatedObject(self, &kTapGestureRecognizerKey);
    if (blcok) {
        blcok(self);
    }
}
@end

4 . NSTimer

#import 

@interface NSTimer (BLBlock)

/**
 *  初始化timer
 *
 *  @param seconds 执行间隔
 *  @param block   执行方法回调
 *  @param repeats 是否重复
 *
 *  @return timer
 */
+ (NSTimer *)bl_scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
                                         block:(void (^)(NSTimer *timer))block
                                       repeats:(BOOL)repeats;

+ (NSTimer *)bl_timerWithTimeInterval:(NSTimeInterval)seconds
                                block:(void (^)(NSTimer *timer))block
                              repeats:(BOOL)repeats;
@end


-----------------------------------------
-----------------------------------------
.m实现文件

#import "NSTimer+BLBlock.h"

@implementation NSTimer (BLBlock)

+ (NSTimer *)bl_scheduledTimerWithTimeInterval:(NSTimeInterval)seconds block:(void (^)(NSTimer *timer))block repeats:(BOOL)repeats {
    return [NSTimer scheduledTimerWithTimeInterval:seconds target:self selector:@selector(execHelperBlock:) userInfo:[block copy] repeats:repeats];
}

+ (NSTimer *)bl_timerWithTimeInterval:(NSTimeInterval)seconds block:(void (^)(NSTimer *timer))block repeats:(BOOL)repeats {
    return [NSTimer timerWithTimeInterval:seconds target:self selector:@selector(execHelperBlock:) userInfo:[block copy] repeats:repeats];
}
@end

5 . UISwitch

#import 

typedef void (^BLSwitchActionBlock)(id sender);

@interface UISwitch (BLBlock)

- (void)bl_handleWithBlock:(BLSwitchActionBlock)actionBlock controlEvent:(UIControlEvents)controlEvent;
@end
-----------------------------------------
-----------------------------------------
.m实现文件
#import "UISwitch+BLBlock.h"
#import 

static char kActionKey;
@implementation UISwitch (BLBlock)

- (void)bl_handleWithBlock:(BLSwitchActionBlock)actionBlock controlEvent:(UIControlEvents)controlEvent{
    
    objc_setAssociatedObject(self, &kActionKey, actionBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(b_callActionBlock:) forControlEvents:controlEvent];
    
}

- (void)b_callActionBlock:(id)sender{
    
    BLSwitchActionBlock block = (BLSwitchActionBlock)objc_getAssociatedObject(self, &kActionKey);
    if (block) {
        block(self);
    }
}
@end

你可能感兴趣的:(UIButton、UIGestureRecongnizer、UITapGestureRecongnizer、NSTimer、UISwitch事件以block的方式回调)