2018-05-25

UIimageView 單擊時(shí)間間隔10s

解決方法寫(xiě)一個(gè)按鈕的分類(lèi),用runtime加個(gè)時(shí)間的屬性,替換按鈕點(diǎn)擊的方法,按鈕寫(xiě)一個(gè)全局的,定義block用于區(qū)分是否是正常的點(diǎn)擊,
[[(AppDelegate*) [UIApplication sharedApplication].delegate floatballButton] sendActionsForControlEvents:UIControlEventTouchUpInside]
替換UIimageView單擊的方法

按鈕的分類(lèi)代碼


#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{
    //獲取這兩個(gè)方法
    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:);
    
    //添加方法進(jìn)去
    BOOL didAddMethod = class_addMethod(self, sysSEL, method_getImplementation(myMethod), method_getTypeEncoding(myMethod));
    
    //如果方法已經(jīng)存在
    if (didAddMethod) {
        class_replaceMethod(self, mySEL, method_getImplementation(systemMethod), method_getTypeEncoding(systemMethod));
    }else{
        method_exchangeImplementations(systemMethod, myMethod);
        
    }
    
    /*-----以上主要是實(shí)現(xiàn)兩個(gè)方法的互換,load是gcd的只shareinstance,果斷保證執(zhí)行一次-------*/
    
}

- (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重復(fù)點(diǎn)擊,設(shè)置間隔 */
@property (nonatomic, assign) NSTimeInterval mm_acceptEventInterval;
@end
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 緩存分為:內(nèi)存緩存 和 數(shù)據(jù)持久化(磁盤(pán)緩存) 內(nèi)存緩存: 內(nèi)存緩存是指當(dāng)前程序運(yùn)行空間,內(nèi)存緩存速度快容量小,它...
    ANN_12閱讀 725評(píng)論 1 0
  • 最新糖果空投信息 一、發(fā)送0.00個(gè)ETH到合約地址免費(fèi)獲得518個(gè)NEP NEP,全稱(chēng)是NePay,一種支付貨幣...
    瑯琊閣主人閱讀 375評(píng)論 0 0
  • 說(shuō)些關(guān)于偏振的事 偏振的分類(lèi) 完全偏振可以使用X和Y方向的兩個(gè)震動(dòng)表示,通常的形式有橢圓偏振,圓偏振,線(xiàn)偏振。使用...
    易碌物閱讀 3,354評(píng)論 0 0
  • 短信權(quán)限 自動(dòng)填寫(xiě)短信驗(yàn)證碼 [圖片上傳中...(image-f5bada-1527212844994-2)] 以...
    開(kāi)心的小哈閱讀 587評(píng)論 1 1
  • 似乎,我抓住了班組的節(jié)奏,不停的輸出對(duì)班組的熱愛(ài),盡量分配工作合理,我也收獲了肯定的聲音,其實(shí)班長(zhǎng)還算好當(dāng)...
    拎著一只四木閱讀 278評(píng)論 0 0

友情鏈接更多精彩內(nèi)容