• iOS开发_分类防止按钮无时间间隔地重复点击


    UIButton+TimeInterval.h

    #import <UIKit/UIKit.h>
    
    @interface UIButton (TimeInterval)
    
    /** 点击时间间隔: 单位(秒) */
    @property (nonatomic, assign) NSTimeInterval timeInterval;
    
    @end
    

    UIButton+TimeInterval.m

    #import "UIButton+TimeInterval.h"
    
    #import <objc/runtime.h>
    
    @interface UIButton ()
    
    /** 是否忽略响应事件 */
    @property(nonatomic, assign) BOOL isIgnoreEvent;
    
    @end
    
    @implementation UIButton (TimeInterval)
    
    /**
     应用启动时,hook住所有按钮的event
     */
    + (void)load {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            SEL selA = @selector(sendAction:to:forEvent:);
            SEL selB = @selector(mySendAction:to:forEvent:);
            Method methodA =   class_getInstanceMethod(self,selA);
            Method methodB = class_getInstanceMethod(self, selB);
            BOOL isAdd = class_addMethod(self, selA, method_getImplementation(methodB), method_getTypeEncoding(methodB));
            if (isAdd) {
                class_replaceMethod(self, selB, method_getImplementation(methodA), method_getTypeEncoding(methodA));
            }
            else {
                method_exchangeImplementations(methodA, methodB);
            }
        });
    }
    
    - (void)mySendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
        if ([NSStringFromClass(self.class) isEqualToString:@"UIButton"]) {
            if (self.isIgnoreEvent) {
                return;
            }
            
            if (self.timeInterval > 0) {
                self.isIgnoreEvent = YES;
                [self performSelector:@selector(setIsIgnoreEvent:) withObject:@(NO) afterDelay:self.timeInterval];
            }
        }
        [self mySendAction:action to:target forEvent:event];
    }
    
    // MARK: - 运行时设置分类属性
    - (NSTimeInterval)timeInterval {
        return [objc_getAssociatedObject(self, _cmd) doubleValue];
    }
    
    - (void)setTimeInterval:(NSTimeInterval)timeInterval {
        objc_setAssociatedObject(self, @selector(timeInterval), @(timeInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (BOOL)isIgnoreEvent {
        return [objc_getAssociatedObject(self, _cmd) boolValue];
    }
    
    - (void)setIsIgnoreEvent:(BOOL)isIgnoreEvent {
        objc_setAssociatedObject(self, @selector(isIgnoreEvent), @(isIgnoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    @end
    

    测试

    #import "ViewController.h"
    
    #import "UIButton+TimeInterval.h"
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        UIButton *btn = [[UIButton alloc] init];
        [self.view addSubview:btn];
        [btn setTitle:@"点我" forState:UIControlStateNormal];
        [btn sizeToFit];
        btn.center = self.view.center;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        // 关键属性,设置每隔多少秒按钮可点
        btn.timeInterval = 2;
    }
    
    - (void)btnClick:(UIButton*)sender{
        NSLog(@"xxxxxx");
    }
    
    @end
    
  • 相关阅读:
    Flask 基础组件(五):请求和响应
    Flask 基础组件(四):模板
    Flask 基础组件(三):路由系统
    Flask 基础组件(二):配置文件
    Flask 基础组件(一):基本使用
    李航统计学习方法(第二版)基本概念(三):统计学习方法三要素
    李航统计学习方法(第二版)基本概念(二):统计学习的分类
    李航统计学习方法(第二版)基本概念(一):统计学习、对象、目的以及方法等
    SQLAlchemy(四):SQLAlchemy查询高级
    CSS Flexible Box Layout: Advanced layouts with flexbox (Flexible Box Layout) – CSS 中文开发手册
  • 原文地址:https://www.cnblogs.com/CH520/p/15566300.html
Copyright © 2020-2023  润新知