防止UIButton快速多次点击导致事件多次触发

项目中遇到了一个Button快速多次点击导致UIControlEventTouchUpInside多次连续触发的问题,这里记录一下解决方案,代码很简单,一个UIButton的分类,当然也可以做UIControl的分类。
UIButton+RepeatTapHandle.h

1
2
3
4
5
#import <UIKit/UIKit.h>
@interface UIButton (RepeatTapHandle)
@end

UIButton+RepeatTapHandle.m

1
2
3
4
5
6
7
8
9
10
11
12
13
#import "UIButton+RepeatTapHandle.h"
@implementation UIButton (RepeatTapHandle)
- (void)sendAction:(SEL)action to:(nullable id)target forEvent:(nullable UIEvent *)event {
[super sendAction:action to:target forEvent:event];
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
});
}
@end

热评文章