iOS开发 防止UIButton,cell等重复点击

来源:互联网 发布:js children 编辑:程序博客网 时间:2024/05/21 19:22

主要是快速点击button或者cell,所对应的action或者逻辑会走多次,例如:点击button或者cell调用拨打电话的方法,会弹出拨打电话框好多次;这个对用户不太友好;问了下哥们儿,他给了个宏,目前算是解决这个问题;代码如下:

// 防止多次调用#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; \}); \

总的思路是设置一个bool变量,记录一下,延时更改下变量的值;使用:在所需要的button或者cell的action前调用即可:

kPreventRepeatClickTime(0.5);
原创粉丝点击