2018-05-25

UIimageView 单击时间间隔10s

解决方法写一个按钮的分类,用runtime加个时间的属性,替换按钮点击的方法,按钮写一个全局的,定义block用于区分是否是正常的点击,
[[(AppDelegate*) [UIApplication sharedApplication].delegate floatballButton] sendActionsForControlEvents:UIControlEventTouchUpInside]
替换UIimageView单击的方法

按钮的分类代码


#import "UIButton+time.h"
#import <objc/runtime.h>

#import "AppDelegate.h"
@implementation UIButton (time)
static const char *UIButton_acceptEventInterval = "UIButton_acceptEventInterval";
static const char *UIButton_acceptEventTime     = "UIButton_acceptEventTime";


- (NSTimeInterval )mm_acceptEventInterval{
    return [objc_getAssociatedObject(self, UIButton_acceptEventInterval) doubleValue];
}

- (void)setMm_acceptEventInterval:(NSTimeInterval)mm_acceptEventInterval{
    objc_setAssociatedObject(self, UIButton_acceptEventInterval, @(mm_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSTimeInterval )mm_acceptEventTime{
    return [objc_getAssociatedObject(self, UIButton_acceptEventTime) doubleValue];
}

- (void)setMm_acceptEventTime:(NSTimeInterval)mm_acceptEventTime{
    objc_setAssociatedObject(self, UIButton_acceptEventTime, @(mm_acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}


+ (void)load{
    //获取这两个方法
    Method systemMethod = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
    SEL sysSEL = @selector(sendAction:to:forEvent:);
    
    Method myMethod = class_getInstanceMethod(self, @selector(mm_sendAction:to:forEvent:));
    SEL mySEL = @selector(mm_sendAction:to:forEvent:);
    
    //添加方法进去
    BOOL didAddMethod = class_addMethod(self, sysSEL, method_getImplementation(myMethod), method_getTypeEncoding(myMethod));
    
    //如果方法已经存在
    if (didAddMethod) {
        class_replaceMethod(self, mySEL, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
    }else{
        method_exchangeImplementations(systemMethod, myMethod);
        
    }
    
    /*-----以上主要是实现两个方法的互换,load是gcd的只shareinstance,果断保证执行一次-------*/
    
}

- (void)mm_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    if (NSDate.date.timeIntervalSince1970 - self.mm_acceptEventTime < self.mm_acceptEventInterval)
    {
        [(AppDelegate*)target clicktoofast];
        return;
    }
    
    if (self.mm_acceptEventInterval > 0) {
        self.mm_acceptEventTime = NSDate.date.timeIntervalSince1970;
    }
    
    [self mm_sendAction:action to:target forEvent:event];
}
@end


#import <UIKit/UIKit.h>

@interface UIButton (time)
/* 防止button重复点击,设置间隔 */
@property (nonatomic, assign) NSTimeInterval mm_acceptEventInterval;
@end
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 缓存分为:内存缓存 和 数据持久化(磁盘缓存) 内存缓存: 内存缓存是指当前程序运行空间,内存缓存速度快容量小,它...
    ANN_12阅读 689评论 1 0
  • 最新糖果空投信息 一、发送0.00个ETH到合约地址免费获得518个NEP NEP,全称是NePay,一种支付货币...
    琅琊阁主人阅读 363评论 0 0
  • 说些关于偏振的事 偏振的分类 完全偏振可以使用X和Y方向的两个震动表示,通常的形式有椭圆偏振,圆偏振,线偏振。使用...
    易碌物阅读 3,292评论 0 0
  • 短信权限 自动填写短信验证码 [图片上传中...(image-f5bada-1527212844994-2)] 以...
    开心的小哈阅读 564评论 1 1
  • 似乎,我抓住了班组的节奏,不停的输出对班组的热爱,尽量分配工作合理,我也收获了肯定的声音,其实班长还算好当...
    拎着一只四木阅读 236评论 0 0