UIControl+recurClick.m 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // UIControl+recurClick.m
  3. // 主要解决按钮的重复点击问题
  4. //
  5. // Created by pican zhang on 2016/12/13.
  6. // Copyright © 2016年 pican. All rights reserved.
  7. //
  8. #import "UIControl+recurClick.h"
  9. #import <objc/runtime.h>
  10. @implementation UIControl (recurClick)
  11. - (NSTimeInterval)uxy_acceptEventInterval
  12. {
  13. return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
  14. }
  15. - (void)setUxy_acceptEventInterval:(NSTimeInterval)uxy_acceptEventInterval
  16. {
  17. objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(uxy_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  18. }
  19. + (void)load
  20. {
  21. Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
  22. Method b = class_getInstanceMethod(self, @selector(__uxy_sendAction:to:forEvent:));
  23. method_exchangeImplementations(a, b);
  24. }
  25. - (void)__uxy_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
  26. {
  27. if (self.uxy_ignoreEvent) return;
  28. if (self.uxy_acceptEventInterval > 0)
  29. {
  30. self.uxy_ignoreEvent = YES;
  31. [self performSelector:@selector(ksksk) withObject:@(NO) afterDelay:self.uxy_acceptEventInterval];
  32. }
  33. [self __uxy_sendAction:action to:target forEvent:event];
  34. }
  35. - (void)ksksk
  36. {
  37. self.uxy_ignoreEvent = NO;
  38. }
  39. - (void)setUxy_ignoreEvent:(BOOL)uxy_ignoreEvent
  40. {
  41. objc_setAssociatedObject(self, BandNameKey, [NSNumber numberWithBool:uxy_ignoreEvent], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  42. }
  43. - (BOOL)uxy_ignoreEvent
  44. {
  45. return [objc_getAssociatedObject(self, BandNameKey) boolValue];
  46. }
  47. @end