- 需求:设置重复点击UIButton的间隔时间,并且间隔时间可以随时更改。
gitHub-Demo - 分析:使用runtime给UIButton关联成员变量< NSTimeInterval repeatEventInterval >,设置其默认时间间隔为0,外界可以通过UIButton创建的对象修改时间间隔。
1.在OC中无法直接给系统的类增加属性和方法,如果给一个类增加成员属性,可以通过runtime给这个类的分类设置关联属性。
2.让UIButton在某个时间点不能响应事件,可以先重写其父类sendAction:to:fromSender:forEvent:方法调用下super的,再写一个方法用来实现控制器按钮是否可以将行为消息转发,然后在load方法中使用runtime交换这两个方法的实现。(当在tabBarController下,UIButton分类中没有重写父类sendAction:to:fromSender:forEvent:方法时,点击tabBarItem会报错:reason: '-[UITabBarButton previousClickTime]: unrecognized selector sent to instance,重写后就解决了)
Snip20161101_1.png
- 资料:UIControl及其子类对于一个给定的事件,会调用UIControl 的 sendAction:to:forEvent:来将行为消息转发到UIApplication对象,再由UIApplication对象调用其sendAction:to:fromSender:forEvent:方法来将消息分发到指定的target上,而如果我们没有指定target,则会将事件分发到响应链上第一个想处理消息的对象上。而如果子类想监控或修改这种行为的话,则可以重写这个方法。http://www.cocoachina.com/ios/20160111/14932.html
3.间隔时间属性:由于分类中@property声明的属性,只会生成set和get方法的声明,不会生成set、get方法的实现及下划线成员属性,可以重写set和get方法,通过runtime在set方法中设置关联,在get方法中获取关联。
4.通过间隔时间控制按钮是否可以点击: 可以通过第一次点击按钮的时间和第二次点击的时间,如果第一次点击按钮的时间 -减- 第二次点击按钮的时间 < 小于 设置的间隔时间,那么就可以点击
代码:
UIButton分类的.h文件
@interface UIButton (ClickBlock)
/** 按钮重复点击的时间间隔,以秒为单位 **/
@property NSTimeInterval repeatEventInterval;
@end
UIButton分类的.m文件
#import "UIButton+ClickBlock.h"
#import <objc/message.h>
const char *repeatEventIntervalKey = "repeatEventIntervalKey";
const char *previousClickTimeKey = "previousClickTimeKey";
@interface UIButton ()
/** 保存1970年到现在的时间(timeIntervalSince1970),时间只会越来越大 */
@property NSTimeInterval previousClickTime; // 不让外界访问
@end
@implementation UIButton (ClickBlock)
+ (void)load {
// 交换方法实现
Method sendAction = class_getInstanceMethod([self class], @selector(sendAction:to:forEvent:));
Method xy_SendAction = class_getInstanceMethod([self class], @selector(xy_sendAction:to:forEvent:));
method_exchangeImplementations(sendAction, xy_SendAction);
}
// // 重写,为了防止在tabBarController下点击tabBarItem时报错
- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
[super sendAction:action to:target forEvent:event];
}
- (void)setRepeatEventInterval:(NSTimeInterval)repeatEventInterval {
objc_setAssociatedObject(self, repeatEventIntervalKey, @(repeatEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSTimeInterval)repeatEventInterval {
NSTimeInterval repeatEventIn = (NSTimeInterval)[objc_getAssociatedObject(self, repeatEventIntervalKey) doubleValue];
// 如果外界设置的重复点击的时间间隔大于0,就按照用户设置的去处理,如果用户设置的间隔时间小于或等于0,就按照无间隔处理
if (repeatEventIn >= 0) {
return repeatEventIn;
}
return 0.0;
}
- (void)setPreviousClickTime:(NSTimeInterval)previousClickTime {
objc_setAssociatedObject(self, previousClickTimeKey, @(previousClickTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSTimeInterval)previousClickTime {
NSTimeInterval previousEventTime = [objc_getAssociatedObject(self, previousClickTimeKey) doubleValue];
if (previousEventTime != 0) {
return previousEventTime;
}
return 1.0;
}
- (void)xy_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
NSTimeInterval time = [[[NSDate alloc] init] timeIntervalSince1970];
if (time - self.previousClickTime < self.repeatEventInterval) {
return;
}
// 如果间隔时间大于0
if (self.repeatEventInterval > 0) {
self.previousClickTime = [[[NSDate alloc] init] timeIntervalSince1970];
}
// 已在load中与系统的sendAction:to:forEvent:方法交换方法实现,所以下面调用的还是系统的方法
[self xy_sendAction:action to:target forEvent:event];
}
@end