[IOS 开发] 防止button被点击多次
2015-08-13 10:10
465 查看
- (void)todoSomething:(id)sender { //在这里做按钮的想做的事情。 } - (void)starButtonClicked:(id)sender { //先将未到时间执行前的任务取消。 [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(todoSomething:) object:sender]; [self performSelector:@selector(todoSomething:) withObject:sender afterDelay:0.2f]; }
说明
iOS常见的延时执行有2种方式
(1)调用NSObject的方法
[self performSelector:@selector(run) withObject:nil afterDelay:2.0];
// 2秒后再调用self的run方法
(2)使用GCD函数
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 2秒后异步执行这里的代码...
});
第一种方法,该方法在那个线程调用,那么run就在哪个线程执行(当前线程),通常是主线程。
[self performSelector:@selector(run) withObject:nil afterDelay:3.0];
如果在子线程中就会有问题. runloop 没有timer
相关文章推荐
- 小白学开发(iOS)OC_ SEL数据类型(2015-08-10)
- iOS语音播放之切换听筒和扬声器
- 小白学开发(iOS)OC_ protocol(协议) (2015-08-09)
- 小白学开发(iOS)OC_ block数据类型(2015-08-08)
- 小白学开发(iOS)OC_ 使用继承来扩充类(2015-08-07)
- 自定义leftBarButtonItem的右滑回退功能首页右滑界面卡死解决
- ios 第一篇文章-xcode6.2键盘调不出来
- ios第三方库类
- iOS开发-oc(菜鸟笔记)
- iOS连载第13篇
- IOS 单元测试
- 适配iOS7 导航栏
- 实现 iOS 后台定位的两种方式
- wxhl iOS bj 49 张浩 学习心得体会连载 第五章
- ios关于frame等知识点的总结一
- IOS——TableView 中利用Item模型进行 Cell 的开发(1)TableView 篇
- iOS设计模式---备忘录模式
- iOS 利用pod_trunk发布程序
- iOS开发学习之自定义cell
- OC_Memory_内存管理原理