What is this problem
Our button is a one-click response, even if the frequent click will not be a problem, but some scenarios will be the problem.
is usually how to solve
We usually set this button to not click when the button is clicked. Wait for 0.xS delay after setting it back; Or the settings can be clicked at the end of the operation.
-(Ibaction) CLICKBTN1: (UIbutton *) sender{ = NO; dosomething = YES;}
If there are different styles of buttons in different states, it is not sufficient to use enabled. Additional variables are added to record the status.
-(Ibaction) CLICKBTN1: (UIbutton *) sender{ ifreturn; = YES; dosomething = NO;}
An example of this is the direct prohibition of clicks directly within the cycle of responding to events. If you want to disable repeated clicks within 1 seconds, you will need to useperformSelector:withObject:afterDelay:
What's a pretty solution?
Having a repeating code snippet is a common denominator that can be abstracted.
We can add a property to the button 重复点击间隔
by setting this property to control the time interval for accepting the click event again.
@interfaceUicontrol (XY) @property (nonatomic, assign) Nstimeinterval uxy_accepteventinterval; //You can use this to repeat the click and interval@endStatic Const Char*uicontrol_accepteventinterval ="Uicontrol_accepteventinterval";-(nstimeinterval) uxy_accepteventinterval{return[Objc_getassociatedobject (Self, uicontrol_accepteventinterval) doublevalue];}- (void) Setuxy_accepteventinterval: (nstimeinterval) uxy_accepteventinterval{objc_setassociatedobject (self, Uicontrol_accepteventinterval, @ (uxy_accepteventinterval), objc_association_retain_nonatomic);}
When the app launches, we hook all the button's event
@implementation Uicontrol (XY)+ (void) load{ = Class_getinstancemethod (self, @selector ( SendAction:to:forEvent:)); = Class_getinstancemethod (self, @selector (__uxy_sendaction:to:forevent:)); Method_exchangeimplementations (A, b);} @end
In our Click event, filter the Click event
-(void) __uxy_sendaction: (SEL) Action to: (ID) target forevent: (uievent *)event{ ifreturn; if 0 ) { = NSDate.date.timeIntervalSince1970; } [Self __uxy_sendaction:action to:target forevent: Event ];}
Actually, that's what it looks like.
UIButton *tempbtn =0.5;
iOS Tips – Use runtime to solve UIButton recurring click problems