iOSはbuttonの繰り返しクリックの問題を解決します

1982 ワード

プロジェクトでは、注文を繰り返し提出する問題がよく発生します.前のように、プロジェクトにはお年玉を奪う機能があります.自分で注文したときも問題ありません.他の人が注文するといつもいろいろなヒントがあります.この問題は少し馬鹿で、必要なスキルでもある.

一般的に私たちが採用している方法は

-(void)buttonClick:(UIbutton*)button{
  button.enabled=No;
  do something(^{
      button.enabled=yes;
});
}```
### , , 

@interface UIControl (FF) @property (nonatomic, assign) NSTimeInterval fy_acceptEventInterval; @property (nonatomic, assign) BOOL fy_ignoreEvent; @end//実現方法
@implementation UIControl (FF) static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval"; static const char *UIControl_ignoreEvent = "UIControl_ignoreEvent";
  • (NSTimeInterval)fy_acceptEventInterval{ return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue]; }
  • (void)setFy_acceptEventInterval:(NSTimeInterval)fy_acceptEventInterval{ objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(fy_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC); }
  • (BOOL)fy_ignoreEvent{ return [objc_getAssociatedObject(self, UIControl_ignoreEvent) boolValue]; }
  • (void)setFy_ignoreEvent:(BOOL)fy_ignoreEvent{ objc_setAssociatedObject(self, UIControl_ignoreEvent, @(fy_ignoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC); }
  • (void)load{ Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:)); Method b = class_getInstanceMethod(self, @selector(_fy_sendAction:to:forEvent:)); method_exchangeImplementations(a, b); }
  • (void)_fy_sendAction:(SEL)selector to:(id)target forEvent:(UIEvent*)event{ if (self.fy_ignoreEvent) return; if (self.fy_acceptEventInterval > 0) { self.fy_ignoreEvent = YES; [self performSelector:@selector(setFy_ignoreEvent:) withObject:@(NO) afterDelay:self.fy_acceptEventInterval]; } [self _fy_sendAction:selector to:target forEvent:event]; } @end
  •  category , (button. fy_acceptEventInterval=2) 。
    ### , @ 。 title , 。