iOS開發- 利用runtime攔截UIButton的點擊事件,防止重複點擊

要是否是很懂的話,能夠直接看上一片關於runtime的介紹。git

應用場景1:坑爹的服務器是否是常常返回一些null數據給你,特別是數組還有字典,而後每次你接收到數據進行轉碼再到model以後,調出model的數組的時候,老是要寫上一些判斷,github

[model.array isKindOfClass:[NSArray class]] && model.array.count > 0)

原則上講是這樣寫比較嚴謹,問題是寫出來的代碼很臃腫,本身都很難接受,我懶!不加上去的話,往NSNull調用不存在的方法,直接Crash,又要被扣工資的節奏。。。最佳解決方法:使用swizzleInstanceMethod。代碼直接見Demo:https://github.com/caijunrong/JRExtension.git數組

 

應用場景2:對於公司原來的一些代碼,想對UIButton的點擊事件作一部分修改,可是若是使用繼承出來的UIBtton來解決的話, 又要改大量的代碼,這時候,使用runtime攔截替換髮送點擊事件的方法能夠迅速解決這個問題,超級給力!服務器

不廢話,直接上代碼ide

//
//  UIControl+UIControl_XY.h
//  iOSanimation
//
//  Created by biyabi on 15/9/29.
//  Copyright © 2015年 caijunrong. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIControl (UIControl_XY)
@property (nonatomic, assign) NSTimeInterval cjr_acceptEventInterval;// 能夠用這個給重複點擊加間隔
@end
//
//  UIControl+UIControl_XY.m
//  iOSanimation
//
//  Created by biyabi on 15/9/29.
//  Copyright © 2015年 caijunrong. All rights reserved.
//

#import "UIControl+UIControl_XY.h"
#import <objc/runtime.h>

@interface UIControl()
@property (nonatomic, assign) NSTimeInterval cjr_acceptEventTime;
@end

@implementation UIControl (UIControl_XY)
static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";
static const char *UIControl_acceptEventTime = "UIControl_acceptEventTime";
- (NSTimeInterval )cjr_acceptEventInterval{
    return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
}

- (void)setCjr_acceptEventInterval:(NSTimeInterval)cjr_acceptEventInterval{
    objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(cjr_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSTimeInterval )cjr_acceptEventTime{
    return [objc_getAssociatedObject(self, UIControl_acceptEventTime) doubleValue];
}

- (void)setCjr_acceptEventTime:(NSTimeInterval)cjr_acceptEventTime{
    objc_setAssociatedObject(self, UIControl_acceptEventTime, @(cjr_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(cjr_sendAction:to:forEvent:));
    SEL mySEL = @selector(cjr_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)cjr_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event{
    if (NSDate.date.timeIntervalSince1970 - self.cjr_acceptEventTime < self.cjr_acceptEventInterval) {
        return;
    }
    
    if (self.cjr_acceptEventInterval > 0) {
        self.cjr_acceptEventTime = NSDate.date.timeIntervalSince1970;
    }
    
    [self cjr_sendAction:action to:target forEvent:event];
}
@end
@interface ALViewController ()
@property (nonatomic, strong) UIButton *;
@end

@implementation AutoLayoutViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.suggessBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.suggessBtn addTarget:self action:@selector(clickWithInterval:) forControlEvents:UIControlEventTouchUpInside];
    self.suggessBtn.cjr_acceptEventInterval = 5.0f;
}

- (void)clickWithInterval:(UIButton *)suButton{
    NSLog(@"打印出來--"):
}
相關文章
相關標籤/搜索