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 <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
相關文章
- Block 我所理解的回撥BloC
- Objective-C中的Block回撥模式ObjectBloC模式
- 網路回撥:Block和Delegate的對比BloC
- [iOS] [OC] 關於block回撥、高階函式“回撥再呼叫”及專案實踐iOSBloC函式
- 前端自定義類事件回撥封裝前端事件封裝
- 回撥函式、訊息和事件例程 (轉)函式事件
- Activity生命週期回撥是如何被回撥的?
- cocos2dx打飛機專案筆記七:各種回撥:定時器schedule、普通回撥callFunc、選單回撥menu_selector、事件回撥event_selector...筆記定時器事件
- [JS]回撥函式和回撥地獄JS函式
- 詳解C#委託,事件與回撥函式C#事件函式
- 網路請求回撥的Block中,到底是用weakSelf還是用self?BloC
- C++中實現回撥機制的幾種方式[轉]C++
- Node.JS呼叫企業微信API:自建應用的回撥事件Node.jsAPI事件
- 基於Guava API實現非同步通知和事件回撥GuavaAPI非同步事件
- 回撥函式的作用函式
- 回撥函式函式
- 微博回撥介面
- java介面回撥Java
- 非同步/回撥非同步
- JS 回撥模式JS模式
- C++回撥C++
- js 回撥 callbackJS
- C++屌屌的觀察者模式-同步回撥和非同步回撥C++模式非同步
- java回撥函式-非同步回撥-簡明講解Java函式非同步
- java 介面回撥經典案例--網路請求回撥Java
- 回撥函式的理解(一)函式
- js中的回撥函式JS函式
- JavaScript中回撥的示例理解JavaScript
- WCF中的非同步回撥非同步
- Javascript的非同步和回撥JavaScript非同步
- Java回撥函式的理解Java函式
- 【詳細、開箱即用】.NET企業微信回撥配置(資料回撥URL和指令回撥URL驗證)
- JavaScript 回撥函式JavaScript函式
- JavaScript回撥函式JavaScript函式
- JS—回撥函式JS函式
- 簡單理解回撥
- 動畫回撥函式動畫函式
- 介面返回前回撥