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

weixin_34292287發表於2016-11-11

實現的方式是給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 <UIKit/UIKit.h>

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 <objc/runtime.h>

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 <UIKit/UIKit.h>

typedef void(^BLGestureRecognizerBlock)(id sender);

@interface UIGestureRecognizer (BLBlock)

- (void)bl_handleRecognizerBlock:(BLGestureRecognizerBlock)actionBlock;
@end
-------------------------------------
-------------------------------------
.m檔案

#import "UIGestureRecognizer+BLBlock.h"
#import <objc/runtime.h>

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 <UIKit/UIKit.h>

typedef void (^BLTapGestureRecognizerBlock)(id sender);

@interface UITapGestureRecognizer (BLBlock)

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

----------------------------------------
----------------------------------------
.m實現檔案
#import "UITapGestureRecognizer+BLBlock.h"
#import <objc/runtime.h>
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 <Foundation/Foundation.h>

@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 <UIKit/UIKit.h>

typedef void (^BLSwitchActionBlock)(id sender);

@interface UISwitch (BLBlock)

- (void)bl_handleWithBlock:(BLSwitchActionBlock)actionBlock controlEvent:(UIControlEvents)controlEvent;
@end
-----------------------------------------
-----------------------------------------
.m實現檔案
#import "UISwitch+BLBlock.h"
#import <objc/runtime.h>

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

相關文章