防止Button重复点击的方法

参考文章
一、使用UIButton的enabled属性, 在点击后, 禁止UIButton的交互, 直到完成指定任务之后再将其enable即可.

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIButton *testButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [_testButton addTarget:self action:@selector(actionFixMultiClick_enabled:) forControlEvents:UIControlEventTouchUpInside];

}
- (void)actionFixMultiClick_enabled:(UIButton *)sender {
    sender.enabled = NO;
    [self btnClickedOperations];
}

- (void)btnClickedOperations {
    self.view.backgroundColor = [UIColor colorWithRed:((arc4random() % 255) / 255.0)
                                                green:((arc4random() % 255) / 255.0)
                                                 blue:((arc4random() % 255) / 255.0)
                                                alpha:1.0f];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"btnClickedOperations");
        _testButton.enabled = YES;
    });
}

二、使用performSelector:withObject:afterDelay:和cancelPreviousPerformRequestsWithTarget

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIButton *testButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
     [_testButton addTarget:self action:@selector(actionFixMultiClick_performSelector:) forControlEvents:UIControlEventTouchUpInside];
  }
- (void)actionFixMultiClick_performSelector:(UIButton *)sender {
    
    NSLog(@"hhhhhhh");
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(btnClickedOperations) object:nil];
    
    [self performSelector:@selector(btnClickedOperations) withObject:nil afterDelay:1];
}

- (void)btnClickedOperations {
    self.view.backgroundColor = [UIColor colorWithRed:((arc4random() % 255) / 255.0)
                                                green:((arc4random() % 255) / 255.0)
                                                 blue:((arc4random() % 255) / 255.0)
                                                alpha:1.0f];

    NSLog(@"啊哈哈f");
}

三、使用runtime来对sendAction:to:forEvent:方法进行hook

首先要了解分类的特性和Rutime机制

1、创建一个UIButton的分类
Button.h文件

#import 

@interface UIButton (YFFixMultiClick)

@property (nonatomic, assign) NSTimeInterval cs_acceptEventInterval; //重复点击的间隔

@property (nonatomic, assign) NSTimeInterval cs_acceptEventTime;

@end

Button.m文件

#import "UIButton+YFFixMultiClick.h"
#import 



@implementation UIButton (YFFixMultiClick)

// 因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);
}

// 在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);
}

- (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];
}

2、实现

#import "ViewController.h"
#import "UIButton+YFFixMultiClick.h"
@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIButton *testButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

     [_testButton addTarget:self action:@selector(actionFixMultiClick_rutime) forControlEvents:UIControlEventTouchUpInside];
      _testButton.cs_acceptEventInterval = 2;
}

-(void)actionFixMultiClick_rutime{

      NSLog(@"哈哈");
}

你可能感兴趣的:(防止Button重复点击的方法)