按钮重复点击问题 UIbutton

2023-05-17,,

.h

<!--
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 {font-variant-ligatures: no-common-ligatures; color: #822e0e}
span.s2 {font-variant-ligatures: no-common-ligatures}
span.s3 {font-variant-ligatures: no-common-ligatures; color: #c32275}
span.s4 {font-variant-ligatures: no-common-ligatures; color: #703daa}
span.s5 {font-variant-ligatures: no-common-ligatures; color: #000000}
-->

#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

<!--
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 {font-variant-ligatures: no-common-ligatures}
span.s2 {font: 18.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures}
span.s3 {font-variant-ligatures: no-common-ligatures; color: #822e0e}
span.s4 {font-variant-ligatures: no-common-ligatures; color: #c32275}
span.s5 {font-variant-ligatures: no-common-ligatures; color: #c91b13}
span.s6 {font-variant-ligatures: no-common-ligatures; color: #703daa}
span.s7 {font-variant-ligatures: no-common-ligatures; color: #000000}
span.s8 {font-variant-ligatures: no-common-ligatures; color: #539aa4}
span.s9 {font-variant-ligatures: no-common-ligatures; color: #0435ff}
span.s10 {font-variant-ligatures: no-common-ligatures; color: #3d1d81}
span.s11 {font-variant-ligatures: no-common-ligatures; color: #6122ae}
span.s12 {font-variant-ligatures: no-common-ligatures; color: #294c50}
-->

//

//  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的相关教程结束。

《按钮重复点击问题 UIbutton.doc》

下载本文的Word格式文档,以方便收藏与打印。