iOS 按钮防止多次快速点击

作者: _Waiting_ | 来源:发表于2021-10-18 10:58 被阅读0次

    iOS 按钮防止多次快速点击,利用runtime交换方法,实现时间控制。

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface UIControl (Extension)
    /** 时间间隔 */
    @property(nonatomic, assign)NSTimeInterval cs_eventInterval;
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "UIControl+Extension.h"
    
    #import <objc/runtime.h>
    
    static char *const kEventIntervalKey = "kEventIntervalKey"; // 时间间隔
    static char *const kEventInvalidKey = "kEventInvalidKey";   // 是否失效
    
    @interface UIControl()
    
    /** 是否失效 - 即不可以点击 */
    @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);
    }
    

    相关文章

      网友评论

        本文标题:iOS 按钮防止多次快速点击

        本文链接:https://www.haomeiwen.com/subject/ylzgoltx.html