美文网首页面试题大全
iOS 限制按钮多次点击

iOS 限制按钮多次点击

作者: 天下林子 | 来源:发表于2018-08-10 15:39 被阅读50次

    代码如下:
    .h文件

    #import <UIKit/UIKit.h>
    
    @interface UIControl (MultiClick)
    
    - (NSTimeInterval)cs_acceptEventInterval;
    
    - (void)setCs_acceptEventInterval:(NSTimeInterval)cs_acceptEventInterval;
    
    - (void)cs_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event;
    
    - (BOOL)cs_acceptEventIntervalFlag;
    
    - (void)setCs_acceptEventIntervalFlag:(BOOL)cs_acceptEventIntervalFlag;
    
    @end
    

    .m文件

    #import "UIControl+MultiClick.h"
    #import <objc/runtime.h>
    @import Aspects;
    @implementation UIControl (MultiClick)
    
    // 因category不能添加属性,只能通过关联对象的方式。
    static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";
    
    - (NSTimeInterval)cs_acceptEventInterval {
        return  [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
    }
    
    - (void)setCs_acceptEventInterval:(NSTimeInterval)cs_acceptEventInterval {
        objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(cs_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    static const char *UIControl_acceptEventTime = "UIControl_acceptEventTime";
    
    - (NSTimeInterval)cs_acceptEventTime {
        return  [objc_getAssociatedObject(self, UIControl_acceptEventTime) doubleValue];
    }
    
    - (void)setCs_acceptEventTime:(NSTimeInterval)cs_acceptEventTime {
        objc_setAssociatedObject(self, UIControl_acceptEventTime, @(cs_acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    // 是否需要设置延时
    static const char *UIControl_acceptEventIntervalFlag = "UIControl_acceptEventIntervalFlag";
    
    - (BOOL)cs_acceptEventIntervalFlag {
        
        return  [objc_getAssociatedObject(self, UIControl_acceptEventIntervalFlag) boolValue];
    }
    
    //YES 防止多次点击
    - (void)setCs_acceptEventIntervalFlag:(BOOL)cs_acceptEventIntervalFlag {
        objc_setAssociatedObject(self, UIControl_acceptEventIntervalFlag, @(cs_acceptEventIntervalFlag), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    // 在load时执行hook
    + (void)load {
        Method before = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
        Method after = class_getInstanceMethod(self, @selector(cs_sendAction:to:forEvent:));
        method_exchangeImplementations(before, after);
        
        [UIControl aspect_hookSelector:@selector(cs_sendAction:to:forEvent:) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> aspectInfo) {
            if ([aspectInfo.instance isKindOfClass:[UIControl class]]) {
                UIControl *instance = aspectInfo.instance;
                NSArray *arguments = aspectInfo.arguments;
                if (arguments.count == 3) {
                    if ([arguments[1] isKindOfClass:[UITabBar class]] || [arguments[1] isKindOfClass:[UITextField class]] || [arguments[1] isKindOfClass:[UITextView class]] || instance.cs_acceptEventIntervalFlag || [instance isKindOfClass:NSClassFromString(@"HMSegmentedControl")] || [arguments[1] isKindOfClass:[UISearchBar class]] || [arguments[1] isKindOfClass:NSClassFromString(@"AVFullScreenPlaybackControlsViewController")] || [arguments[1] isKindOfClass:NSClassFromString(@"CAMViewfinderViewController")]) {
                        instance.cs_acceptEventInterval = 0;
                    } else {//默认1秒内点击一次
                        instance.cs_acceptEventInterval = 1;
                    }
                }
            }
        } error:nil];
    }
    
    - (void)cs_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
        if ([NSDate date].timeIntervalSince1970  - self.cs_acceptEventTime < self.cs_acceptEventInterval) {
            return;
        }
        if (self.cs_acceptEventInterval > 0) {
            self.cs_acceptEventTime = [NSDate date].timeIntervalSince1970;
        }
        [self cs_sendAction:action to:target forEvent:event];
    }
    
    @end
    
    

    相关文章

      网友评论

        本文标题:iOS 限制按钮多次点击

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