iOS 防止Button或Cell快速重複點擊多次響應事件

方法一

1.首先定義一個BOOL類型來判斷是否點擊了第一次:

@property (nonatomic, assign) BOOL isSelect;

設置self.isSelect = false;

2.然後在點擊事件中這樣寫:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //防止重複點擊
   if (self.isSelect == false) {
        self.isSelect = true;
        //在延時方法中將isSelect更改爲false
       [self performSelector:@selector(repeatDelay) withObject:nil afterDelay:0.5f]; 
       // TODO:在下面實現點擊cell需要實現的邏輯就可以了
        
}

3.延時

- (void)repeatDelay{
      self.isSelect = false;
}

方法二

1.宏定義:

// 防止多次調用
#define kPreventRepeatClickTime(_seconds_) \
static BOOL shouldPrevent; \
if (shouldPrevent) return; \
shouldPrevent = YES; \
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((_seconds_) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ \
shouldPrevent = NO; \
}); \

2.在所需要的button或者cell的action前調用即可:

kPreventRepeatClickTime(0.5);

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章