iOS Tips – Use runtime to solve UIButton recurring click problems

Source: Internet
Author: User
Tags uicontrol

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.