In real development, when a button button is clicked, an event is triggered to execute. Under normal operation, the button responds to a single click. However, if the user clicks a button more than once, the event will be executed more than once, resulting in a bug. How to solve this problem gracefully? Today we’re going to use the Runtime to solve the problem of UIButton double clicking. I’m going to create a new category that inherits from UIControl, and I’m going to name it myself. UIControl + ZHW. H (.h files)

@interface UIControl (ZHW) @property (nonatomic, assign) NSTimeInterval zhw_acceptEventInterval; @property (nonatomic, assign) BOOL zhw_ignoreEvent; // Whether to ignore the click event and not respond to the click event @endCopy the code

UIControl+ zhw. m(.m file) To use Runtime, import the corresponding libraries (objc/ Runtime.h).

@implementation UIControl (ZHW)
static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";

static const char *UIcontrol_ignoreEvent = "UIcontrol_ignoreEvent";

- (NSTimeInterval)zhw_acceptEventInterval {

    return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];

}

- (void)setZhw_acceptEventInterval:(NSTimeInterval)zhw_acceptEventInterval {

    objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(zhw_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

- (BOOL)zhw_ignoreEvent {

    return [objc_getAssociatedObject(self, UIcontrol_ignoreEvent) boolValue];

}

- (void)setZhw_ignoreEvent:(BOOL)zhw_ignoreEvent {

    objc_setAssociatedObject(self, UIcontrol_ignoreEvent, @(zhw_ignoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

+ (void)load {

    Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));

    Method b = class_getInstanceMethod(self, @selector(__zhw_sendAction:to:forEvent:));

    method_exchangeImplementations(a, b);

}

- (void)__zhw_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {

    if (self.zhw_ignoreEvent) return;

    if (self.zhw_acceptEventInterval > 0) {

        self.zhw_ignoreEvent = YES;

        [self performSelector:@selector(setZhw_ignoreEvent:) withObject:@(NO) afterDelay:self.zhw_acceptEventInterval];

    }

    [self __zhw_sendAction:action to:target forEvent:event];

}

@endCopy the code

Import the UIControl+ zhw. h header file where needed to set the button click interval

@interface ViewController () @property (weak, nonatomic) IBOutlet UIButton *button; @property (weak, nonatomic) IBOutlet UIView *colorView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.button.zhw_ignoreEvent = NO; The self. The button. Zhw_acceptEventInterval = 3.0; } - (IBAction)runtimeAction:(UIButton *)sender { NSLog(@"----run click"); [UIView animateWithDuration:3 animations:^{ self.colorView.center = CGPointMake(200, 500);  } completion:^(BOOL finished) { self.colorView.center = CGPointMake(95, 85); }]; } - (IBAction)buttonAction:(UIButton *)sender { NSLog(@"------comm click"); [UIView animateWithDuration:3 animations:^{ self.colorView.center = CGPointMake(200, 500);  } completion:^(BOOL finished) { self.colorView.center = CGPointMake(95, 85); }]; }Copy the code

Running the demo, you can see that the problem of button multiple clicks has been solved. In setting the button’s corresponding click event interval, the button will respond to only one click event during this interval. Attached is demo: UIButtonMutablieClick