UIControl+JXCustom.m 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // UIControl+JXCustom.m
  3. // shiku_im
  4. //
  5. // Created by daxiong on 17/12/29.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "UIControl+JXCustom.h"
  9. #import <objc/runtime.h>
  10. @interface UIControl()
  11. @property (nonatomic, assign) NSTimeInterval custom_acceptEventTime;
  12. @end
  13. @implementation UIControl (JXCustom)
  14. + (void)load{
  15. static dispatch_once_t onceToken;
  16. dispatch_once(&onceToken, ^{
  17. Method systemMethod = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
  18. // SEL sysSEL = @selector(sendAction:to:forEvent:);
  19. Method customMethod = class_getInstanceMethod(self, @selector(custom_sendAction:to:forEvent:));
  20. SEL customSEL = @selector(custom_sendAction:to:forEvent:);
  21. //添加方法 语法:BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types) 若添加成功则返回No
  22. // cls:被添加方法的类 name:被添加方法方法名 imp:被添加方法的实现函数 types:被添加方法的实现函数的返回值类型和参数类型的字符串
  23. BOOL didAddMethod = class_addMethod(self, @selector(sendAction:to:forEvent:), method_getImplementation(customMethod), method_getTypeEncoding(customMethod));
  24. //如果系统中该方法已经存在了,则替换系统的方法 语法:IMP class_replaceMethod(Class cls, SEL name, IMP imp,const char *types)
  25. if (didAddMethod) {
  26. class_replaceMethod(self, customSEL, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
  27. }else{
  28. method_exchangeImplementations(systemMethod, customMethod);
  29. }
  30. });
  31. }
  32. - (NSTimeInterval )custom_acceptEventInterval{
  33. return [objc_getAssociatedObject(self, "UIControl_acceptEventInterval") doubleValue];
  34. }
  35. - (void)setCustom_acceptEventInterval:(NSTimeInterval)custom_acceptEventInterval{
  36. objc_setAssociatedObject(self, "UIControl_acceptEventInterval", @(custom_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  37. }
  38. - (NSTimeInterval )custom_acceptEventTime{
  39. return [objc_getAssociatedObject(self, "UIControl_acceptEventTime") doubleValue];
  40. }
  41. - (void)setCustom_acceptEventTime:(NSTimeInterval)custom_acceptEventTime{
  42. objc_setAssociatedObject(self, "UIControl_acceptEventTime", @(custom_acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  43. }
  44. - (void)custom_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
  45. // 如果想要设置统一的间隔时间,可以在此处加上以下几句
  46. // 值得提醒一下:如果这里设置了统一的时间间隔,会影响UISwitch,如果想统一设置,又不想影响UISwitch,建议将UIControl分类,改成UIButton分类,实现方法是一样的
  47. // if (self.custom_acceptEventInterval <= 0) {
  48. // // 如果没有自定义时间间隔,则默认为2秒
  49. // self.custom_acceptEventInterval = 2;
  50. // }
  51. // 是否小于设定的时间间隔
  52. BOOL needSendAction = (NSDate.date.timeIntervalSince1970 - self.custom_acceptEventTime >= self.custom_acceptEventInterval);
  53. // 更新上一次点击时间戳
  54. if (self.custom_acceptEventInterval > 0) {
  55. self.custom_acceptEventTime = NSDate.date.timeIntervalSince1970;
  56. }
  57. // 两次点击的时间间隔小于设定的时间间隔时,才执行响应事件
  58. if (needSendAction) {
  59. [self custom_sendAction:action to:target forEvent:event];
  60. }
  61. }
  62. @end