iOS using runtime resolution UIButton multiple clicks (repeat)

Source: Internet
Author: User
Tags uicontrol

In actual development, when we click on a button button, we need to trigger an event to execute. When the user is under normal operation, the button will only respond to a single click when clicked. But if a user clicks a button more than once, it causes the event to be executed multiple times, causing some bugs to appear.
How to solve this problem gracefully. Today we're going to use runtime to solve the problem of UIButton repeated clicks.
First create a new category category, inherit from Uicontrol, and define the name yourself.
Uicontrol+zhw.h (. h file)

@interface Uicontrol (ZHW)
@property (nonatomic, assign) Nstimeinterval zhw_accepteventinterval;//add a time interval between click events

@property (nonatomic, assign) BOOL zhw_ignoreevent;//ignores click events and does not respond to click events
@end

UICONTROL+ZHW.M (. m file) when using runtime, you need to import the appropriate library (objc/runtime.h)

@implementation Uicontrol (ZHW) static const char *uicontrol_accepteventinterval = "Uicontrol_accepteventinterval";

static const char *uicontrol_ignoreevent = "uicontrol_ignoreevent"; -(Nstimeinterval) zhw_accepteventinterval {return [Objc_getassociatedobject (self, uicontrol_accepteventinterval) do

Ublevalue]; }-(void) Setzhw_accepteventinterval: (nstimeinterval) Zhw_accepteventinterval {objc_setassociatedobject (self, UICon

Trol_accepteventinterval, @ (zhw_accepteventinterval), objc_association_retain_nonatomic);

}-(BOOL) zhw_ignoreevent {return [Objc_getassociatedobject (self, uicontrol_ignoreevent) boolvalue]; }-(void) Setzhw_ignoreevent: (BOOL) zhw_ignoreevent {objc_setassociatedobject (self, uicontrol_ignoreevent, @ (zhw_ign

oreevent), objc_association_retain_nonatomic);

    } + (void) Load {Method A = Class_getinstancemethod (self, @selector (sendAction:to:forEvent:)); Method B = Class_getinstancemethod (self, @selector (__zhw_sendaction:to:forevent:));

    Method_exchangeimplementations (A, b);

    }-(void) __zhw_sendaction: (SEL) action to: (ID) Target forevent: (Uievent *) event {if (self.zhw_ignoreevent) return;

        if (Self.zhw_accepteventinterval > 0) {self.zhw_ignoreevent = YES;

    [Self performselector: @selector (setzhw_ignoreevent:) withobject:@ (NO) afterDelay:self.zhw_acceptEventInterval];

[Self __zhw_sendaction:action to:target forevent:event]; } @end

Set the button's click time interval when you need to import Uicontrol+zhw.h header files

@interface Viewcontroller ()
@property (weak, nonatomic) Iboutlet UIButton *button;
@property (Weak, nonatomic) Iboutlet UIView *colorview;

@end

@implementation Viewcontroller

-(void) viewdidload {
    [super viewdidload];
    Self.button.zhw_ignoreEvent = NO;
    Self.button.zhw_acceptEventInterval = 3.0;
}
-(Ibaction) Runtimeaction: (UIButton *) Sender {
    NSLog (@ "----Run click");
    [UIView animatewithduration:3 animations:^{

        self.colorView.center = cgpointmake (+);

    } completion:^ ( BOOL finished) {

        self.colorView.center = Cgpointmake ();}

    ];
}
-(Ibaction) Buttonaction: (UIButton *) Sender {
    NSLog (@ "------Comm Click");
    [UIView animatewithduration:3 animations:^{

        self.colorView.center = cgpointmake (+);

    } completion:^ ( BOOL finished) {

        self.colorView.center = Cgpointmake ();}

    ];
}

Run the demo, you can find the button multiple clicks to solve the problem. At the time interval at which the corresponding click event of the button is set, the button responds to only one click event during this interval.

Attached Demo:uibuttonmutablieclick

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.