一、用于记录控制器事件
其实可以在控制器的viewWillAppear、viewDidLoad等方法中添加追踪代码来实现,但是需要很多的重复代码;使用继承同样的也会有不少重复代码。如果用分类来进行方法交换:
// 通过class_getInstanceMethod()函数从当前对象中的method list获取method结构体,如果是类方法就使用class_getClassMethod()函数获取。
Method fromMethod = class_getInstanceMethod([self class], @selector(viewDidLoad));
MethodtoMethod =class_getInstanceMethod([selfclass],@selector(swizzlingViewDidLoad));
/**
* 我们在这里使用class_addMethod()函数对Method Swizzling做了一层验证,如果self没有实现被交换的方法,会导致失败。
* 而且self没有交换的方法实现,但是父类有这个方法,这样就会调用父类的方法,结果就不是我们想要的结果了。
* 所以我们在这里通过class_addMethod()的验证,如果self实现了这个方法,class_addMethod()函数将会返回NO,我们就可以对其进行交换了。
*/
if (!class_addMethod([self class], @selector(swizzlingViewDidLoad), method_getImplementation(toMethod), method_getTypeEncoding(toMethod))) {
method_exchangeImplementations(fromMethod, toMethod);
}
二、防止按钮重复点击
创建按钮的分类
.声明文件(.h)
#define defaultInterval .5//默认时间间隔
@interface UIButton (UIbutton_Delay)
@property(nonatomic,assign)NSTimeInterval timeInterval;//用这个给重复点击加间隔
@property(nonatomic,assign)BOOL isIgnoreEvent;//YES不允许点击NO允许点击
@end
.实现文件(.m)
#import <objc/runtime.h>
@implementation UIButton (UIbutton_Delay)
- (NSTimeInterval)timeInterval{
return[objc_getAssociatedObject(self,_cmd)doubleValue];
}
- (void)setTimeInterval:(NSTimeInterval)timeInterval{
objc_setAssociatedObject(self,@selector(timeInterval),@(timeInterval),OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
//runtime动态绑定属性
- (void)setIsIgnoreEvent:(BOOL)isIgnoreEvent{
objc_setAssociatedObject(self,@selector(isIgnoreEvent),@(isIgnoreEvent),OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)isIgnoreEvent{
return[objc_getAssociatedObject(self,_cmd)boolValue];
}
- (void)resetState{
[self setIsIgnoreEvent:NO];
}
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL selA =@selector(sendAction:to:forEvent:);
SEL selB =@selector(mySendAction:to:forEvent:);
Method methodA =class_getInstanceMethod(self, selA);
Method methodB =class_getInstanceMethod(self, selB);
//将methodB的实现添加到系统方法中也就是说将methodA方法指针添加成方法methodB的返回值表示是否添加成功
BOOL isAdd =class_addMethod(self, selA,method_getImplementation(methodB),method_getTypeEncoding(methodB));
//添加成功了说明本类中不存在methodB所以此时必须将方法b的实现指针换成方法A的,否则b方法将没有实现。
if(isAdd) {
class_replaceMethod(self, selB,method_getImplementation(methodA),method_getTypeEncoding(methodA));
}else{
//添加失败了说明本类中有methodB的实现,此时只需要将methodA和methodB的IMP互换一下即可。
method_exchangeImplementations(methodA, methodB);
}
});
}
- (void)mySendAction:(SEL)action to:(id)target forEvent:(UIEvent*)event{
if([NSStringFromClass(self.class)isEqualToString:@"UIButton"]) {
self.timeInterval=self.timeInterval==0?defaultInterval:self.timeInterval;
if(self.isIgnoreEvent){
return;
}else if(self.timeInterval>0){
[self performSelector:@selector(resetState) withObject:nil afterDelay:self.timeInterval];
}
}
//此处methodA和methodB方法IMP互换了,实际上执行sendAction;所以不会死循环
self.isIgnoreEvent=YES;
[self mySendAction:action to:target forEvent:event];
}
三、增大按钮的点击热区
.h 文件
#import <UIKit/UIKit.h>
@interface UIButton (HitAreaExpand)
@property (nonatomic) CGFloat minHitTestWidth;
@property (nonatomic) CGFloat minHitTestHeight;
@end
.m 文件
#import "UIButton+HitAreaExpand.h"
#import <objc/runtime.h>
@implementation UIButton (HitAreaExpand)
- (CGFloat)minHitTestWidth {
NSNumber * width = objc_getAssociatedObject(self, @selector(minHitTestWidth));
return [width floatValue];
}
- (void)setMinHitTestWidth:(CGFloat)minHitTestWidth {
objc_setAssociatedObject(self, @selector(minHitTestWidth), [NSNumber numberWithFloat:minHitTestWidth], OBJC_ASSOCIATION_ASSIGN);
}
- (CGFloat)minHitTestHeight {
NSNumber * height = objc_getAssociatedObject(self, @selector(minHitTestHeight));
return [height floatValue];
}
- (void)setMinHitTestHeight:(CGFloat)minHitTestHeight {
objc_setAssociatedObject(self, @selector(minHitTestHeight), [NSNumber numberWithFloat:minHitTestHeight], OBJC_ASSOCIATION_ASSIGN);
}
- (BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event {
return CGRectContainsPoint(HitTestingBounds(self.bounds, self.minHitTestWidth, self.minHitTestHeight), point);
}
CGRect HitTestingBounds(CGRect bounds, CGFloat minimumHitTestWidth, CGFloat minimumHitTestHeight) {
CGRect hitTestingBounds = bounds;
if (minimumHitTestWidth > bounds.size.width) {
hitTestingBounds.size.width = minimumHitTestWidth;
hitTestingBounds.origin.x -= (hitTestingBounds.size.width - bounds.size.width)/2;
}
if (minimumHitTestHeight > bounds.size.height) {
hitTestingBounds.size.height = minimumHitTestHeight;
hitTestingBounds.origin.y -= (hitTestingBounds.size.height - bounds.size.height)/2;
}
return hitTestingBounds;
}
四、防止数组越界造成奔溃(该方法需要注意类簇)
@implementation NSArray (Limit)
+ (void)load {
//[super load];
//可变数组方法调换
Method fromMethod = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(objectAtIndexedSubscript:));
Method toMethod = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(cm_objectAtIndexedSubscript:));
method_exchangeImplementations(fromMethod, toMethod);
//不可变数组方法调换
Method a = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(objectAtIndex:));
Method b = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(cm_objectAtIndex:));
method_exchangeImplementations(a, b);
}
- (id)cm_objectAtIndexedSubscript:(NSUInteger)index {
// 判断下标是否越界,如果越界就进入异常拦截
if (self.count-1 < index) {
@try {
return nil;
}
@catch (NSException *exception) {
// 在崩溃后会打印崩溃信息
NSLog(@"---------- %s 奔溃信息 Method ----------\n", class_getName(self.class));
NSLog(@"%@", [exception callStackSymbols]);
return nil;
}
@finally {}
} // 如果没有问题,则正常进行方法调用
else {
return [self cm_objectAtIndexedSubscript:index];
}
}
- (id)cm_objectAtIndex:(NSUInteger)index {
// 判断下标是否越界,如果越界就进入异常拦截
if (self.count-1 < index) {
@try {
return nil;
}
@catch (NSException *exception) {
// 在崩溃后会打印崩溃信息
NSLog(@"---------- %s 奔溃信息 Method ----------\n", class_getName(self.class));
NSLog(@"%@", [exception callStackSymbols]);
return nil;
}
@finally {}
} // 如果没有问题,则正常进行方法调用
else {
return [self cm_objectAtIndex:index];
}
}
@end