swift 3.0 Dispatch_once 代替 按钮的连续点击处理 OC版处理。

这两个都是借鉴别人的,想让自己记忆更深一点,所以写在简书。

now,开始

Paste_Image.png

按钮连续点击的处理。

Paste_Image.png

下面是oc版本。意思都是一样的。


#import <UIKit/UIKit.h>

@interface UIControl (recurClick)
@property (nonatomic, assign) NSTimeInterval uxy_acceptEventInterval;
@property (nonatomic, assign)BOOL uxy_ignoreEvent;
@end
static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";
static const void *BandNameKey = &BandNameKey;
#import "UIControl+recurClick.h"
#import <objc/runtime.h>


@implementation UIControl (recurClick)
- (NSTimeInterval)uxy_acceptEventInterval
{
    return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
}
- (void)setUxy_acceptEventInterval:(NSTimeInterval)uxy_acceptEventInterval
{
    objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(uxy_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
   
}
+ (void)load
{
    Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
    Method b = class_getInstanceMethod(self, @selector(__uxy_sendAction:to:forEvent:));
    method_exchangeImplementations(a, b);
}
- (void)__uxy_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
    if (self.uxy_ignoreEvent) return;
    if (self.uxy_acceptEventInterval > 0)
    {
        self.uxy_ignoreEvent = YES;
        
        [self performSelector:@selector(ksksk) withObject:@(NO) afterDelay:self.uxy_acceptEventInterval];
    }
    [self __uxy_sendAction:action to:target forEvent:event];
}

- (void)ksksk
{
    self.uxy_ignoreEvent = NO;
}
- (void)setUxy_ignoreEvent:(BOOL)uxy_ignoreEvent
{
    
    objc_setAssociatedObject(self, BandNameKey, [NSNumber numberWithBool:uxy_ignoreEvent], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)uxy_ignoreEvent
{
    
    return [objc_getAssociatedObject(self, BandNameKey) boolValue];
}

@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容