iOS UIButton 防止連續點擊(最后一次才去響應事件)

方案一

- (void)buttonClickMethod:(UIButton *)sender {
    
    // 每次點擊的時候都會先取消一次響應,然后調用perform方法,延遲響應該事件,避免多次響應
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(delayResponseMethod) object:btn];
    // 延遲執行,一般設置1秒左右,太久會顯得有延遲,響應也不能太慢
    [self performSelector:@selector(delayResponseMethod) withObject:btn afterDelay:1.0];
}
- (void)delayResponseMethod {
    NSLog(@"延遲執行的方法");
}

方案二

  • 通過NSTimer延遲執行方法,和方案一類似
//定時器
@property (nonatomic, strong) NSTimer *timer;//定時器
@property(nonatomic, assign) NSInteger count;
//需要給count一個默認值,比如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 防止滾動的時候有延遲*/
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
- (void)delayResponseMethod {
    NSLog(@"延遲執行的方法");
}

方案三

通過Runtime交換UIButton的響應事件方法,從而控制響應事件的時間間隔。

1 創建一個UIButton的分類,使用runtime增加public屬性cs_eventInterval和private屬性cs_eventInvalid。
2 在+load方法中使用runtime將UIButton的-sendAction:to:forEvent:方法與自定義的cs_sendAction:to:forEvent:方法進行交換
3 使用cs_eventInterval作為控制cs_eventInvalid的計時因子,用cs_eventInvalid控制UIButton的event事件是否有效。
*代碼實現如下

@interface UIButton (Extension)

/** 時間間隔 */
@property(nonatomic, assign)NSTimeInterval cs_eventInterval;

@end

import "UIButton+Extension.h"

import <objc/runtime.h>

static char *const kEventIntervalKey = "kEventIntervalKey"; // 時間間隔
static char *const kEventInvalidKey = "kEventInvalidKey"; // 是否失效

@interface UIButton()

/** 是否失效 - 即不可以點擊 */
@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);
    }
    測試代碼如下
    /** 方法三 */

  • (void)drawExpecialBtn{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
    [btn setTitle:@"按鈕點擊" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    // 按鈕不可點擊時,文字顏色置灰
    [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(@"按鈕點擊...");
    }

  • 在方法三中交互UIButton的sendAction:to:forEvent:方法,實際上交互的是UIControl的sendAction:to:forEvent:方法,所以在使用·UIControl·或其·子類(比如UISlider)·的·sendAction:to:forEvent:·方法時會引起參數缺失的崩潰。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。