UIButton is a commonly used control in our iOS development, and continuous/jitter clicks are also common in users! After the project found that the online solution to this experience problem is quite a lot of information, but still have to make a note, convenient next time to check!
Programme one:
-(void) viewdidload{ [Super Viewdidload]; // 1. Create BTN and add click event UIButton *btn = [UIButton buttonwithtype:uibuttontypecustom]; [Btn Setframe:cgrectmake (+)]; = [Uicolor greencolor]; [Btn addtarget:self Action: @selector (CLICKBTN:) forcontrolevents:uicontroleventtouchupinside]; [Self.view addsubview:btn];}
//2. The delay time is set to T (here is set to 1.0s), so the pre-and two-click time interval of no more than 1.0s will be canceled- (void) CLICKBTN: (UIButton *) btn{NSLog (@"%s", __function__); [ Selfclass] cancelpreviousperformrequestswithtarget:self selector: @selector (hand Leevent:)Object: BTN]; [Self performselector: @selector (handleevent:) withobject:btn afterdelay:1.0];}//3. How to handle the Click event- (void) Handleevent: (UIButton *) btn{NSLog (@"%s", __function__);}
Scenario Two: Add classification to UIButton, so that all UIButton instances in the project can be conveniently used!
Main code:
// . h file #import <UIKit/UIKit.h>@interface UIButton (clickeventtime) @property (nonatomic, assign) Nstimeinterval Custom_accepteventinterval; @end
//. m file#import "uibutton+clickeventtime.h"#import<objc/runtime.h>@implementationUIButton (clickeventtime)/** 1. Interception System Method 2. Replace the system method of the first step interception with a custom method 3. In the custom method, try to terminate the previous response to successive clicks*/#pragmaMark-----Interception System Method + (void) load{SEL Syssel=@selector (sendAction:to:forEvent:); Method Systemmethod=Class_getinstancemethod (self, syssel); SEL Customsel=@selector (custom_sendAction:to:forEvent:); Method Custommethod=Class_getinstancemethod (self, customsel); Method_exchangeimplementations (Systemmethod, Custommethod); }#pragmaMark-----A custom method for replacing system methods-(void) Custom_sendaction: (SEL) action to: (ID) Target forevent: (Uievent *)Event{ //is less than the set time intervalBOOL needsendaction = (nsdate.date.timeintervalsince1970-self.custom_accepteventtime >=self.custom_accepteventinterval); //Update Last click Time Stamp if(Self.custom_accepteventinterval >0) {Self.custom_accepteventtime=NSDate.date.timeIntervalSince1970; } if(needsendaction) {//response events are performed only when the time interval of two clicks is less than the set time interval[Self custom_sendaction:action to:target forevent:Event]; }Else{ return; }}-(nstimeinterval) custom_accepteventtime{return[Objc_getassociatedobject (Self,"Uicontrol_accepteventtime") Doublevalue];}- (void) Setcustom_accepteventtime: (nstimeinterval) custom_accepteventtime{objc_setassociatedobject (self,"Uicontrol_accepteventtime", @ (custom_accepteventtime), objc_association_retain_nonatomic); }#pragmaMark------Association-(nstimeinterval) custom_accepteventinterval{return[Objc_getassociatedobject (Self,"Uicontrol_accepteventinterval") Doublevalue];}- (void) Setcustom_accepteventinterval: (nstimeinterval) custom_accepteventinterval{objc_setassociatedobject (self, "Uicontrol_accepteventinterval", @ (custom_accepteventinterval), objc_association_retain_nonatomic);}@end
Programme II references
Load and initialize methods for NSObject
IOS---Understand the runtime mechanism and its usage scenarios
Ios-uibutton Prevent continuous clicks (click Jitter)