1.先实现按钮点击功能,先写好了按钮,在触发按钮的方法中做如下操作:
-(void)BtnAction:(UIButton *)btn{ //按钮点击
// 在0.2秒时间间隔内多次点击只响应一次点击事件, todoSomething就是我们具体要实现的方法
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(todoSomething:) object:btn]; [self performSelector:@selector(todoSomething:) withObject:btn afterDelay:0.2f]; }
-(void)todoSomething:(UIButton *)btn{
//这里面写timer,timer调自己的方法
}
2.到这里就结束了,关键的就那一步:
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(todoSomething:) object:btn]; [self performSelector:@selector(todoSomething:) withObject:btn afterDelay:0.2f];
希望对大家有所帮助。
本文介绍了一种防止iOS应用中按钮被快速重复点击的方法。通过使用cancelPreviousPerformRequestsWithTarget和performSelector来确保在设定的时间间隔内即使多次点击按钮也只会触发一次响应。
9312

被折叠的 条评论
为什么被折叠?



