問題:
主要是快速點擊button或者cell,所對應(yīng)的action或者邏輯會走多次,例如:點擊button或者cell調(diào)用撥打電話的方法,會彈出撥打電話框好多次;這個對用戶不太友好
解決方案:
方法一
1.首先定義一個BOOL類型來判斷是否點擊了第一次:
@property (nonatomic, assign) BOOL isSelect;
2.然后在點擊事件中這樣寫:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//防止重復(fù)點擊
if (self.isSelect == false) {
self.isSelect = true;
//在延時方法中將isSelect更改為false
[self performSelector:@selector(repeatDelay) withObject:nil afterDelay:0.5f];
// TODO:在下面實現(xiàn)點擊cell需要實現(xiàn)的邏輯就可以了
}
3.延時
- (void)repeatDelay{
self.isSelect = false;
}
方法二
1.宏定義:
// 防止多次調(diào)用
#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前調(diào)用即可:
kPreventRepeatClickTime(0.5);