In actual development, when we click on a button button, we need to trigger an event to execute. When the user is under normal operation, the button will only respond to a single click when clicked. But if a user clicks a button more than once, it causes the event to be executed multiple times, causing some bugs to appear.
How to solve this problem gracefully. Today we're going to use runtime to solve the problem of UIButton repeated clicks.
First create a new category category, inherit from Uicontrol, and define the name yourself.
Uicontrol+zhw.h (. h file)
@interface Uicontrol (ZHW)
@property (nonatomic, assign) Nstimeinterval zhw_accepteventinterval;//add a time interval between click events
@property (nonatomic, assign) BOOL zhw_ignoreevent;//ignores click events and does not respond to click events
@end
UICONTROL+ZHW.M (. m file) when using runtime, you need to import the appropriate library (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) do
Ublevalue]; }-(void) Setzhw_accepteventinterval: (nstimeinterval) Zhw_accepteventinterval {objc_setassociatedobject (self, UICon
Trol_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_ign
oreevent), 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]; } @end
Set the button's click time interval when you need to import Uicontrol+zhw.h header files
@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;
Self.button.zhw_acceptEventInterval = 3.0;
}
-(Ibaction) Runtimeaction: (UIButton *) Sender {
NSLog (@ "----Run click");
[UIView animatewithduration:3 animations:^{
self.colorView.center = cgpointmake (+);
} completion:^ ( BOOL finished) {
self.colorView.center = Cgpointmake ();}
];
}
-(Ibaction) Buttonaction: (UIButton *) Sender {
NSLog (@ "------Comm Click");
[UIView animatewithduration:3 animations:^{
self.colorView.center = cgpointmake (+);
} completion:^ ( BOOL finished) {
self.colorView.center = Cgpointmake ();}
];
}
Run the demo, you can find the button multiple clicks to solve the problem. At the time interval at which the corresponding click event of the button is set, the button responds to only one click event during this interval.
Attached Demo:uibuttonmutablieclick