美文网首页iOS开发之笔记摘录
避免短时间多次点击按钮之iOS笔记摘录

避免短时间多次点击按钮之iOS笔记摘录

作者: 平安喜乐698 | 来源:发表于2020-03-31 18:24 被阅读0次
    目录
    
    
    

    春眠不觉晓,处处闻啼鸟。 夜来风雨声,花落知多少。

    1. 使用

    1. 创建分类文件

    UIButton+FQTimeButton.h

    #import <UIKit/UIKit.h>
    
    #define defaultInterval 2.0// 默认间隔时间
    
    @interface UIButton (FQTimeButton)
    // 设置点击时间间隔
    @property (nonatomic, assign) NSTimeInterval timeInterVal;
    @end
    
    

    UIButton+FQTimeButton.m

    #import "UIButton+FQTimeButton.h"
    
    @interface UIButton()
    // 用来判断 是否去执行方法
    @property (nonatomic, assign) BOOL isExcuteEvent;
    @end
    
    
    @implementation UIButton (FQTimeButton)
    
    // main方法之前会调用d所有类的load方法
    + (void)load{
        // 仅执行一次,避免多次替换出错
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            
            // 交换自定义方法和系统方法
            SEL oldSel = @selector(sendAction:to:forEvent:);
            SEL newSel = @selector(newSendAction:to:forEvent:);
            Method oldMethod = class_getInstanceMethod(self, oldSel);
            Method newMethod = class_getInstanceMethod(self, newSel);
            BOOL isAdd = class_addMethod(self, oldSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
            if (isAdd) {
                class_replaceMethod(self, newSel, method_getImplementation(oldMethod), method_getTypeEncoding(oldMethod));
            }else{
                method_exchangeImplementations(oldMethod, newMethod);
            }
        });
    }
    
    // 触发按钮事件时会调用
    - (void)newSendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
        if ([NSStringFromClass(self.class) isEqualToString:@"UIButton"]) {
            if (self.isExcuteEvent == 0) {
                self.timeInterVal = self.timeInterVal = 0? defaultInterval:self.timeInterVal;
            }
            if (self.isExcuteEvent) return;
            if (self.timeInterVal > 0) {
                self.isExcuteEvent = true;
                [self performSelector:@selector(setIsExcuteEvent:) withObject:nil afterDelay:self.timeInterVal];
            }
        }
        [self newSendAction:action to:target forEvent:event];
    }
    
    // 动态关联属性
    - (NSTimeInterval)timeInterVal{
        return [objc_getAssociatedObject(self, _cmd) doubleValue];
    }
    - (void)setTimeInterVal:(NSTimeInterval)timeInterVal{
        objc_setAssociatedObject(self, @selector(timeInterVal), @(timeInterVal), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    - (void)setIsExcuteEvent:(BOOL)isExcuteEvent{
        objc_setAssociatedObject(self, @selector(isExcuteEvent), @(isExcuteEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    - (BOOL)isExcuteEvent{
        return [objc_getAssociatedObject(self, _cmd) boolValue];
    }
    @end
    
    1. 使用
    #import "UIButton+FQTimeButton.h"
    
    -(UIButton *)saveButton{
        if(!_saveButton){
            _saveButton=[UIButton new];
            [_saveButton setTimeInterVal:3.5];    // 设置3.5秒内不可重复点击
            [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
            [_saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_saveButton.titleLabel setFont:kFontBold(14)];
            [_saveButton setBackgroundColor:[UIColor colorWithHex:0xFB5752]];
            [_saveButton.layer setMasksToBounds:true];
            [_saveButton.layer setCornerRadius:kEstimateWidth(50)/2.0];
            [_saveButton addTarget:self action:@selector(fq_handleSave:) forControlEvents:UIControlEventTouchUpInside];
        }
        return _saveButton;
    }
    

    相关文章

      网友评论

        本文标题:避免短时间多次点击按钮之iOS笔记摘录

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