iOS tips-using runtime to solve UIButton repeated click problems

Source: Internet
Author: User

iOS tips – Using runtime to solve UIButton recurring Click problems 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{    sender.enabledNO;    doSomething    sender.enabledYES;}

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.

 @interface uicontrol (XY)@property(nonatomic,Assign)NstimeintervalUxy_accepteventinterval;//You can use this to repeat the click Plus 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{    Method a = class_getInstanceMethod(self@selector(sendAction:to:forEvent:));    Method b = 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{if( Self. Uxy_ignoreevent)return;if( Self. Uxy_accepteventinterval>0)    { Self. Uxy_ignoreevent=YES; [ SelfPerformselector:@selector(setuxy_ignoreevent:) withobject:@ (NO) Afterdelay: Self. Uxy_accepteventinterval]; }    [ Self__uxy_sendaction:action to:target forevent:event];}

Actually, that's what it looks like.

    UIButton *tempBtn = [UIButton buttonWithType:UIButtonTypeCustom];    [tempBtn addTarget:self action:@selector(clickWithInterval:) forControlEvents:UIControlEventTouchUpInside];    tempBtn.uxy_acceptEventInterval0.5;

This is the end of the article. Although it is not recommended to use runtime in a wide range, small-scale operation can solve many small problems.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

iOS tips-using runtime to solve UIButton repeated click problems

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.