方案一
- (void)buttonClickMethod:(UIButton *)sender {
// 每次點(diǎn)擊的時(shí)候都會(huì)先取消一次響應(yīng),然后調(diào)用perform方法叙量,延遲響應(yīng)該事件,避免多次響應(yīng)
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayResponseMethod) object:btn];
// 延遲執(zhí)行夕晓,一般設(shè)置1秒左右宛乃,太久會(huì)顯得有延遲,響應(yīng)也不能太慢
[self performSelector:@selector(delayResponseMethod) withObject:btn afterDelay:1.0];
}
- (void)delayResponseMethod {
NSLog(@"延遲執(zhí)行的方法");
}
方案二
- 通過(guò)NSTimer延遲執(zhí)行方法蒸辆,和方案一類似
//定時(shí)器
@property (nonatomic, strong) NSTimer *timer;//定時(shí)器
@property(nonatomic, assign) NSInteger count;
//需要給count一個(gè)默認(rèn)值征炼,比如0
- (void)buttonClickMethod:(UIButton *)sender {
self.count ++;
[self.timer invalidate];
self.timer = nil;
self.timer =[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(delayResponseMethod) userInfo:nil repeats:NO];
/* NSRunLoopCommonModes 防止?jié)L動(dòng)的時(shí)候有延遲*/
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)delayResponseMethod {
NSLog(@"延遲執(zhí)行的方法");
}
方案三
通過(guò)Runtime交換UIButton的響應(yīng)事件方法,從而控制響應(yīng)事件的時(shí)間間隔躬贡。
- 轉(zhuǎn)載自CSDN博主「walkerwqp」的文章谆奥,如有問(wèn)題,聯(lián)系本人可刪除
- 原文鏈接:https://blog.csdn.net/walkerwqp/java/article/details/92561515
實(shí)現(xiàn)步驟如下:
1 創(chuàng)建一個(gè)UIButton的分類拂玻,使用runtime增加public屬性cs_eventInterval和private屬性cs_eventInvalid酸些。
2 在+load方法中使用runtime將UIButton的-sendAction:to:forEvent:方法與自定義的cs_sendAction:to:forEvent:方法進(jìn)行交換
3 使用cs_eventInterval作為控制cs_eventInvalid的計(jì)時(shí)因子,用cs_eventInvalid控制UIButton的event事件是否有效檐蚜。
*代碼實(shí)現(xiàn)如下
@interface UIButton (Extension)
/** 時(shí)間間隔 */
@property(nonatomic, assign)NSTimeInterval cs_eventInterval;
@end
import "UIButton+Extension.h"
import <objc/runtime.h>
static char *const kEventIntervalKey = "kEventIntervalKey"; // 時(shí)間間隔
static char *const kEventInvalidKey = "kEventInvalidKey"; // 是否失效
@interface UIButton()
/** 是否失效 - 即不可以點(diǎn)擊 */
@property(nonatomic, assign)BOOL cs_eventInvalid;
@end
@implementation UIButton (Extension)
- (void)load {
// 交換方法
Method clickMethod = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
Method cs_clickMethod = class_getInstanceMethod(self, @selector(cs_sendAction:to:forEvent:));
method_exchangeImplementations(clickMethod, cs_clickMethod);
}
pragma mark - click
- (void)cs_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
if (!self.cs_eventInvalid) {
self.cs_eventInvalid = YES;
[self cs_sendAction:action to:target forEvent:event];
[self performSelector:@selector(setCs_eventInvalid:) withObject:@(NO) afterDelay:self.cs_eventInterval];
}
}
pragma mark - set | get
(NSTimeInterval)cs_eventInterval {
return [objc_getAssociatedObject(self, kEventIntervalKey) doubleValue];
}(void)setCs_eventInterval:(NSTimeInterval)cs_eventInterval {
objc_setAssociatedObject(self, kEventIntervalKey, @(cs_eventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}(BOOL)cs_eventInvalid {
return [objc_getAssociatedObject(self, kEventInvalidKey) boolValue];
}(void)setCs_eventInvalid:(BOOL)cs_eventInvalid {
objc_setAssociatedObject(self, kEventInvalidKey, @(cs_eventInvalid), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
測(cè)試代碼如下
/** 方法三 */(void)drawExpecialBtn{
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
[btn setTitle:@"按鈕點(diǎn)擊" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
// 按鈕不可點(diǎn)擊時(shí),文字顏色置灰
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
btn.center = self.view.center;
[btn addTarget:self action:@selector(tapBtn:) forControlEvents:UIControlEventTouchUpInside];
btn.cs_eventInterval = 2.0;
[self.view addSubview:btn];
}(void)tapBtn:(UIButton *)btn {
NSLog(@"按鈕點(diǎn)擊...");
}在方法三中交互UIButton的sendAction:to:forEvent:方法,實(shí)際上交互的是UIControl的sendAction:to:forEvent:方法魄懂,所以在使用·UIControl·或其·子類(比如UISlider)·的·sendAction:to:forEvent:·方法時(shí)會(huì)引起參數(shù)缺失的崩潰。