iOS dynamic Modify button response time

Source: Internet
Author: User

In the development of the project we may encounter such a situation, such as when we log in to send data to the server for comparison, usually we do when the user click on the button, use a load effect of the view to block the current interface until the server returns data or timeout. If you do not block, the user may frequently click to log in, and you continue to send data, this is obviously not the letter, to solve this way there are many ways.

Today we say a way to make the button response time controlled by itself.
To achieve this effect you may need to know what Runtime

OK, if you do not know very well, there is not much to use for this function. These include:

1Objc_getassociatedobject (<#ID Object#>, <#Const void*key#>) 2 parameter one: usually self, caller3Parameter two: Your Key (Key-value)4 5 6 There's a get method, so there's definitely a set .7 8Objc_setassociatedobject (<#ID Object#>, <#Const void*key#>, <#IDValue#&gt, < #objc_AssociationPolicy policy#>)9 parameter one: selfTen parameter two: Key One parameter Three: value, it is important to note that packaging becomes an ID type for an int, float, and other basic types A parameter four: MRC-based Stuff - /* - objc_association_assign = 0, the objc_association_retain_nonatomic = 1, - objc_association_copy_nonatomic = 3, - Objc_association_retain = 01401, - objc_association_copy = 01403 + */ -Why the above method is used, because the property is added dynamically to a category.


After we have finished adding properties dynamically, we need to use the nature of the runtime to move our hands and feet on the system's methods.

Here we also need to know three methods are:

This method can get the number of the system object method, the type is Methodclass_getinstancemethod (< #__unsafe_unretained Class Cls#>, < #SEL Name#>) This method can be used to obtain the number of the system class method, the type is Methodclass_getclassmethod (< #__unsafe_unretained class Cls#> < #SEL name#>) Here we're going to get the method of the object, so with the first, after acquiring the method of the system, we need to define our own method, which is used interchangeably with the system method to interchange the number of two methods Method_ Exchangeimplementations (< #Method M1#>, < #Method m2#>): Before and after swap


After Exchange

So I have achieved the purpose of the exchange.


Now that we know a few of the methods above, we can start writing code.

First, if you want to use the runtime first step you need to import:
  #import <objc/message.h>


The second step is to use the runtime dynamic to add attributes

This is to override the Get Set method.
By KVC assignment, we first have to have a key to store/fetch the data

Static Char*ConstPq_accepttimekey ="Pq_accepttime";Static Char*ConstPq_delayintervalkey ="Pq_delaybuttoninterval";//Getter Method-(nstimeinterval) pq_delaybuttoninterval{return[Objc_getassociatedobject (Self, pq_delayintervalkey) doublevalue];}-(nstimeinterval) pq_accepttime{return[Objc_getassociatedobject (Self, pq_accepttimekey) doublevalue];}//Setter Method- (void) Setpq_delaybuttoninterval: (nstimeinterval) Pq_delaybuttoninterval{objc_setassociatedobject (self, PQ_ Delayintervalkey, @ (pq_delaybuttoninterval), objc_association_retain_nonatomic);}- (void) Setpq_accepttime: (nstimeinterval) Pq_accepttime{objc_setassociatedobject (self, Pq_accepttimekey, @ (pq_ Accepttime), objc_association_retain_nonatomic);}



After we have finished adding properties dynamically, we need to swap the system's methods with our methods in order to add functionality.

1+ (void) load{2 Staticdispatch_once_t Oncetoken;3Dispatch_once (&oncetoken, ^{4Method Systemmethod = Class_getinstancemethod ([selfclass], @selector (sendAction:to:forEvent:));5Method Myselfmethod = Class_getinstancemethod ([selfclass], @selector (pq_sendAction:to:forEvent:));6BOOL Isadd = Class_addmethod ([selfclass], @selector (sendAction:to:forEvent:), Method_getimplementation (Myselfmethod), method_gettypeencoding ( Myselfmethod));7 if(!Isadd) {8 method_exchangeimplementations (Systemmethod, myselfmethod);9 }Ten }); One}

Then we have to write our own method, here is a small trick, first put the system's method name, such as for this method can be done:
1. First input-sendaction generally hit here the system will prompt automatic completion

2. After obtaining the system's method name, add our prefix in front of it, indicating that it is our own method. Like what


Here I add a prefix PQ, which is then connected with an underscore.
-(void) Pq_sendaction: (SEL) action to: (ID) Target forevent: (Uievent *) event

The next step is to handle the implementation of the Code

1 The two variables used here need to be exposed externally (in. h files), one written in (. m files)2 @interfaceUicontrol ()3 4 @property (nonatomic,assign) nstimeinterval pq_accepttime;5 6 @end7 8- (void) Pq_sendaction: (SEL) action to: (ID) Target forevent: (Uievent *)Event{9 if(Nsdate.date.timeintervalsince1970-self.pq_accepttime <self.pq_delaybuttoninterval) {TenNSLog (@"I don't bird you now."); One return; A } - if(Self.pq_delaybuttoninterval >0) { -Self.pq_accepttime =NSDate.date.timeIntervalSince1970; the } -[Self pq_sendaction:action to:target forevent:Event]; -}

at this point, the function is complete, the small partners can immediately use the


Create a button yourself, and then set the following code, if you find that you do not find your own definition of the genus , then you generally do not import the header file in the Viewcontroller
Setting this delay time is 5 seconds
Self.clickMeBtn.pq_delayButtonInterVal = 5;


It's all done here, the code word is not easy, if you feel OK, trouble top! Reprint please indicate the source, thank you.

iOS dynamic Modify button response time

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.