首页 > 代码库 > 按钮重复点击问题 UIbutton

按钮重复点击问题 UIbutton

.h

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c32275 } span.s1 { color: #822e0e } span.s2 { } span.s3 { color: #c32275 } span.s4 { color: #703daa } span.s5 { color: #000000 }</style>

#import <UIKit/UIKit.h>

#import <objc/runtime.h>

@interface UIControl (XY)

 

@property (nonatomic, assign) NSTimeInterval fy_acceptEventInterval;

@property (nonatomic, assign) BOOL fy_ignoreEvent;

 

@end

 

.m

<style>p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #1d9421 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; min-height: 21.0px } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c91b13 } p.p4 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo } p.p5 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #3d1d81 } p.p6 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #539aa4 } p.p7 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Menlo; color: #c32275 } span.s1 { } span.s2 { font: 18.0px "PingFang SC" } span.s3 { color: #822e0e } span.s4 { color: #c32275 } span.s5 { color: #c91b13 } span.s6 { color: #703daa } span.s7 { color: #000000 } span.s8 { color: #539aa4 } span.s9 { color: #0435ff } span.s10 { color: #3d1d81 } span.s11 { color: #6122ae } span.s12 { color: #294c50 }</style>

//

//  UIControl+XY.m

//  Test

//

//  Created by xueshan on 17/7/3.

//  Copyright ? 2017 xueshan. All rights reserved.

//

 

#import "UIControl+XY.h"

 

@implementation UIControl (XY)

 

static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";

static const char *UIControl_ignoreEvent = "UIControl_ignoreEvent";

 

- (NSTimeInterval)fy_acceptEventInterval{

    return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];

}

- (void)setFy_acceptEventInterval:(NSTimeInterval)fy_acceptEventInterval{

    objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(fy_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

 

- (BOOL)fy_ignoreEvent{

    return [objc_getAssociatedObject(self, UIControl_ignoreEvent) boolValue];

}

 

- (void)setFy_ignoreEvent:(BOOL)fy_ignoreEvent{

    objc_setAssociatedObject(self, UIControl_ignoreEvent, @(fy_ignoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

 

+ (void)load{

    Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));

    Method b = class_getInstanceMethod(self, @selector(_fy_sendAction:to:forEvent:));

    method_exchangeImplementations(a, b);

}

 

- (void)_fy_sendAction:(SEL)selector to:(id)target forEvent:(UIEvent*)event{

    if (self.fy_ignoreEvent) return;

    

    if (self.fy_acceptEventInterval > 0) {

        self.fy_ignoreEvent = YES;

        [self performSelector:@selector(setFy_ignoreEvent:) withObject:@(NO) afterDelay:self.fy_acceptEventInterval];

    }

    

    [self _fy_sendAction:selector to:target forEvent:event];

}

 

@end

按钮重复点击问题 UIbutton