iOS之使用Runtime解決UIButton多次點擊(重複點擊)

來源:互聯網
上載者:User

在實際開發中我們,點擊一個button按鍵時,需要觸發一個事件去執行。使用者在正常操作情況下,單次點擊時,button只會響應一次點擊。但是如果使用者多次點擊一個button,那麼就會引起這個事件被多次執行,導致一些bug的出現。
如何優雅解決的這個問題呢。今天我們來使用Runtime來解決UIButton重複點擊的問題。
 首先建立一個分類category,繼承於UIControl,名字自己定義。
 UIControl+ZHW.h(.h檔案)

@interface UIControl (ZHW)@property (nonatomic, assign) NSTimeInterval zhw_acceptEventInterval;//添加點擊事件的間隔時間@property (nonatomic, assign) BOOL zhw_ignoreEvent;//是否忽略點擊事件,不響應點擊事件@end

UIControl+ZHW.m(.m檔案)在使用runtime時,需要匯入相應的庫(objc/runtime.h)

@implementation UIControl (ZHW)static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";static const char *UIcontrol_ignoreEvent = "UIcontrol_ignoreEvent";- (NSTimeInterval)zhw_acceptEventInterval {    return [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];}- (void)setZhw_acceptEventInterval:(NSTimeInterval)zhw_acceptEventInterval {    objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(zhw_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);}- (BOOL)zhw_ignoreEvent {    return [objc_getAssociatedObject(self, UIcontrol_ignoreEvent) boolValue];}- (void)setZhw_ignoreEvent:(BOOL)zhw_ignoreEvent {    objc_setAssociatedObject(self, UIcontrol_ignoreEvent, @(zhw_ignoreEvent), OBJC_ASSOCIATION_RETAIN_NONATOMIC);}+ (void)load {    Method a = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));    Method b = class_getInstanceMethod(self, @selector(__zhw_sendAction:to:forEvent:));    method_exchangeImplementations(a, b);}- (void)__zhw_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {    if (self.zhw_ignoreEvent) return;    if (self.zhw_acceptEventInterval > 0) {        self.zhw_ignoreEvent = YES;        [self performSelector:@selector(setZhw_ignoreEvent:) withObject:@(NO) afterDelay:self.zhw_acceptEventInterval];    }    [self __zhw_sendAction:action to:target forEvent:event];}@end

 在需要用到地方匯入UIControl+ZHW.h標頭檔設定button的點擊時間間隔

@interface ViewController ()@property (weak, nonatomic) IBOutlet UIButton *button;@property (weak, nonatomic) IBOutlet UIView *colorView;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    self.button.zhw_ignoreEvent = NO;    self.button.zhw_acceptEventInterval = 3.0;}- (IBAction)runtimeAction:(UIButton *)sender {    NSLog(@"----run click");    [UIView animateWithDuration:3 animations:^{        self.colorView.center = CGPointMake(200, 500);    } completion:^(BOOL finished) {        self.colorView.center = CGPointMake(95, 85);    }];}- (IBAction)buttonAction:(UIButton *)sender {    NSLog(@"------comm click");    [UIView animateWithDuration:3 animations:^{        self.colorView.center = CGPointMake(200, 500);    } completion:^(BOOL finished) {        self.colorView.center = CGPointMake(95, 85);    }];}

 運行demo,可以發現button多次點擊的問題得到瞭解決。在設定button的相應點擊事件的時間間隔,在這個 間隔時間內,button只會響應一次點擊事件。
 
附上demo:UIButtonMutablieClick

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.